socket网络编程注意事项

今天上了socket网络编程,照着课本上的代码敲了一遍,居然发现了好多错误?当然其中不乏自己敲错了,但是真的书上好多坑,学校的这本书真的好辣鸡

以下是我找朋友修改后的代码

client端

#include
#include 
#include 
#include
#include 
#include 
#include 
#include 
#include 
#define PORT 4321
#define BUFFER_SIZE 1024

int main(int argc,char *argv[]){
	int sockfd,sendbytes;
	char buf[BUFFER_SIZE];
	struct hostent *host;
	struct sockaddr_in serv_addr;
	if(argc<3){
		fprintf(stderr, "USAGE:./client Hostname(or ip address)Text\n");
		exit(1);
	}
	memset(buf,0,sizeof(buf));
	sprintf(buf,"%s",argv[2]);

	if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1){
		perror("socket");
		exit(1);
	}
	//printf("Socket id=%d\n",socket);
	serv_addr.sin_family=AF_INET;
	serv_addr.sin_port=htons(PORT);
	//serv_addr.sin_addr=*((struct in_addr*)host->h_addr);
	serv_addr.sin_addr.s_addr=htonl(INADDR_ANY);
	bzero(&(serv_addr.sin_zero),8);
	if(connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(struct sockaddr))==-1){
		perror("connect");
		exit(1);
	}
	if((sendbytes=send(sockfd,buf,strlen(buf),0))==-1){
		perror("send");
		exit(1);
	}
	close(sockfd);
	exit(0);
	//int i=1;
	//setsockopt(sockfd,SOL_SOCKET,SO_REUSERADDR,&i,sizeof(i));
	/*
	if(bind(sockfd,(struct sockaddr *)&server_sockaddr,sizeof(struct sockaddr))==-1){
		perror("bind");
		exit(1);
	}
	printf("Bind success!\n");
	if(listen(sockfd,MAX_QUE_CONN_NM)==-1){
		perror("listen");
		exit(1);
	}
	printf("Listening....\n");
	if((client_fd=accept(sockfd,(struct sockaddr *)&client_sockaddr,&sin_size))==-1){
		perror("accept");
		exit(1);
	}
	memset(buf,0,sizeof(buf));
	if((recvbytes=recv(client_fd,buf,BUFFER_SIZE,0))==-1){
		perror("recv");
		exit(1);
	}
	printf("Recived a message:%s\n",buf );
	close(sockfd);
	exit(0);
	*/
}

server端

#include
#include 
#include 
#include
#include 
#include 
#include 
#include 
#define PORT 4321
#define BUFFER_SIZE 1024
#define MAX_QUE_CONN_NM 5
int main(){
	struct sockaddr_in server_sockaddr,client_sockaddr;
	int sin_size,recvbytes;
	int sockfd,client_fd;
	char buf[BUFFER_SIZE];
	if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1){
		perror("socket");
		exit(1);
	}
	printf("Socket id=%d\n",socket);
	server_sockaddr.sin_family=AF_INET;
	server_sockaddr.sin_port=htons(PORT);
	server_sockaddr.sin_addr.s_addr=INADDR_ANY;
	bzero(&(server_sockaddr.sin_zero),8);
	int i=1;
	setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&i,sizeof(i));
	if(bind(sockfd,(struct sockaddr *)&server_sockaddr,sizeof(struct sockaddr))==-1){
		perror("bind");
		exit(1);
	}
	printf("Bind success!\n");
	if(listen(sockfd,MAX_QUE_CONN_NM)==-1){
		perror("listen");
		exit(1);
	}
	printf("Listening....\n");
	if((client_fd=accept(sockfd,(struct sockaddr *)&client_sockaddr,&sin_size))==-1){
		perror("accept");
		exit(1);
	}
	memset(buf,0,sizeof(buf));
	if((recvbytes=recv(client_fd,buf,BUFFER_SIZE,0))==-1){
		perror("recv");
		exit(1);
	}
	printf("Recived a message:%s\n",buf );
	close(sockfd);
	exit(0);
}

然后打开两个终端,分别输入

//第一步
gcc -o server server.c
gcc -o client client.c
//第二步
./server
./client localhost hello

最终结果如下:

Socket id=906817216
Bind success!
Listening....
Recived a message:hello

这个版本与原来的代码相比差在client.c的第32行代码

//serv_addr.sin_addr=*((struct in_addr*)host->h_addr);
	serv_addr.sin_addr.s_addr=htonl(INADDR_ANY);

 

你可能感兴趣的:(socket网络编程注意事项)