求救:C语言改错题...200分奉上,急!!!(200分)

  • 主题发起人 主题发起人 jaicky_lee
  • 开始时间 开始时间
J

jaicky_lee

Unregistered / Unconfirmed
GUEST, unregistred user!
C语言服务器和客户端改错程序1例,源程序附下:
=====================================================================
一、Server端
代码:
#include <winsock.h>
#include <stdio.h>
#define TRUE 1
#define PROTOPORT	3000
#define QLEN		6		//size of request queue
int visits = 0;
main(argc,argv)
int argc;
char *argv[];
{
	struct	hostent *ptrh;			//pointer to a host table entry
	struct	protoent *ptrp;			//pointer to a protocol table entry
	struct	sockaddr_in *sad;		//server address
	struct	sockaddr_in cad;		//client address
	int		sd,sd2;					//socket变量,sd负责listen,sd2负责accept
	int		port;					//端口
	int		alen;					//addr length
	char	buf[1024],buf1[1024];				
	int		rval;
	//初始化win32环境
	WSADATA wsaData;
	WSAStartup(0x0101,&amp;wsaData);
	//初始化sad
	memset((char*)&amp;sad,0,sizeof(sad));
	sad.sin_family=AF_INET;
	sad.sin_addr.s_addr=INADDR_ANY;
	if (argc>1)	{					//如果用参数定义了端口,使用参数
		port=atoi(argv[1]);			//convert argument to binary 将参数转换
	}else
{
		port=PROTOPORT;				//否则使用缺省端口
	}
	if(port>0)
		sad.sin_port=htons((u_short)port);	//赋端口(字节序转换)
	else
{
		fprintf(stderr,"bad port number %s/n",argv[1]);
		exit(1);
	}
	//获得TCP协议号
	if ( ((int)(ptrp=getprotobyname("tcp")))==0){
		fprintf(stderr,"cannot map /"tcp/" to protocol number");
		exit(1);
	}

	//建立socket
	sd=socket(AF_INET,SOCK_STREAM,ptrp.p_proto);
	if (sd<0)
	{
		perror("openning stream socket");
		exit(1);
	}
	
	//绑定socket
	
	if (bind(sd,sad,sizeof(sad))<0)
	{
		perror("binding stream socket");
		exit(1);
	}
	//找到指定端口并显示
	alen=sizeof(sad);
	if (getsockname(sd,sad,&amp;alen)<0)
	{
		perror("getting socket name");
		exit(1);
	}
	printf("net port # %d /n",ntohs(sad.sin_port));
	//begin
 listenning
	if (listen(sd,QLEN)<0){			//5为最大可连接的client数,若为SOMAXCONN,则由系统自行定义
		perror("listenning");
		exit(1);
	}

	//处理client连接
	while(TRUE)
	{
		alen=sizeof(cad);
		sd2=accept(sd,sad,&amp;alen);
		if (sd2=-1) perror("accept");
		else
do
 {
			memset(buf,0,sizeof(buf));
			rval=recv(sd2,buf,sizeof(buf),0);
			if (rval<0) perror("reading stream message");
			if (rval==0) printf("ending connection/n");
			else
 {
				printf("%s/n",buf);
			
				//memset(buf,0,sizeof(buf));
				//scanf("%s",buf);
				//send(sd2,buf,strlen(buf),0);
				memset(buf1,0,sizeof(buf1));
				scanf("%s",buf1);
				send(sd,buf,strlen(buf),0);
			}
		}while (rval!=0);
		closesocket(sd2);
	}
	exit(0);
}

========================================================
二、Client端
代码:
#include <winsock.h>
#include <stdio.h>
#define PROTOPORT	3000
extern int errno;
char localhost[]="localhost";

main(argc,argv)
int argc;
char *argv[];
{
	struct	hostent *ptrh;			//pointer to a host table entry
	struct	protoent *ptrp;			//pointer to a protocol table entry
	struct	sockaddr_in sad;		//server address
	int		sd;						//socket变量,sd负责listen,sd2负责accept
	int		port;					//端口
	char	*host;					//pointer to host name
	int		n;						//number of char read
	char	buf[1024],buf1[1024];				
	
	//初始化win32环境
	WSADATA wsaData;
	WSAStartup(0x0101,&amp;wsaData);
	//初始化sad
	memset(sad,0,sizeof(sad));
	sad.sin_family=AF_INET;
	sad.sin_addr.s_addr=INADDR_ANY;
	
	if (argc>2)	{					//如果用参数定义了端口,使用参数
		port=atoi(argv[2]);			//convert argument to binary 将参数转换
	}else
{
		port=PROTOPORT;				//否则使用缺省端口
	}
	if(port>0)
		sad.sin_port=htons((u_short)port);	//赋端口
	else
{
		fprintf(stderr,"bad port number %s/n",argv[2]);
		exit(1);
	}
	if (argc>1){
		host=argv[1];				//如果使用参数定义了主机,使用参数
	}else
{
		host=localhost;
	}
	ptrh=gethostbyname(host);
	if( ((char*)ptrh)==NULL){
		fprintf(stderr,"invalid host: %s/n",host);//进行主机->地址转换
		exit(1);
	}
	memcpy(sad.sin_addr,ptrh.h_addr,ptrh.h_length);

	//获得TCP协议号
	if ( ((int)(ptrp=getprotobyname("tcp")))==0){
		fprintf(stderr,"cannot map /"tcp/" to protocol number");
		exit(1);
	}

	//建立socket
	sd=socket(AF_INET,SOCK_STREAM,ptrp.p_proto);
	if (sd<0)
	{
		perror("openning stream socket");
		exit(1);
	}
	//连接服务器
	if (connect(sd,sad,sizeof(sad))<0){
		fprintf(stderr,"connect failed/n");
		exit(1);
	}
	printf("server connected/n");
	memset(buf,0,sizeof(buf));
	scanf("%s",buf);
	send(sd,buf,strlen(buf),0);
	
	//处理连接后的数据传输
	do{
		memset(buf,0,sizeof(buf));
		if (recv(&amp;sd,buf,sizeof(buf),0)<0) perror("reading stream message");
		else
 {
			printf("-->%s/n",buf);
			scanf("%s",buf);
			send(sd,buf,strlen(buf),0);
		}
	}while(1);
	closesocket(sd);
	
	exit(0);
}
 
后退
顶部