server.c #include<stdio.h> #include<sys/socket.h> #include<sys/types.h> #include<netinet/in.h> #include<netdb.h> #include<stdlib.h> #include<string.h> #define PORT 8000 int main() { struct sockaddr_in sin; struct sockaddr_in pin; int sock_main,tmp_socket,add_size; char buf[16384]; int i; char str[]={"hello baby~"}; if((sock_main=socket(AF_INET,SOCK_STREAM,0))==-1) { perror("Failed socket"); exit(1); } bzero(&sin,sizeof(sin)); sin.sin_family=AF_INET; sin.sin_addr.s_addr=INADDR_ANY; sin.sin_port=htons(PORT); if(bind(sock_main,(struct sockaddr*)&sin,sizeof(sin))==-1) { perror("failed bind"); exit(1); } if(listen(sock_main,10)==-1) { perror("failed listen"); exit(1); } printf("start to wait accept connections..../n"); tmp_socket=accept(sock_main,(struct sockaddr*)&pin,&add_size); if(tmp_socket==-1) { perror("failed accept"); exit(1); } if(send(tmp_socket,str,strlen(str)+1,0)==-1) { perror("failed send"); exit(1); } if(recv(tmp_socket,buf,16384,0)==-1) { perror("failed recv"); exit(1); } printf("received from client:/n%s/n",buf); close(tmp_socket); close(sock_main); exit(0); } switch.c #include<stdio.h> #include<sys/socket.h> #include<netinet/in.h> #include<netdb.h> #include<string.h> #include<stdlib.h> #include<arpa/inet.h> #define HOST_NAME "192.168.157.28" #define BUF_SIZE 4096 int main(int argc ,char *argv[]) { int socket_s,socket_s1,tmp_socket,add_size; struct sockaddr_in pin1; struct sockaddr_in sin; struct sockaddr_in pin; struct hostent *s_host_info; char buf[8192]; if((s_host_info=gethostbyname(HOST_NAME))==0) { perror("failed gethostname/n"); exit(1); } bzero(&pin1,sizeof(pin1));//用于和server pin1.sin_family=AF_INET; pin1.sin_addr.s_addr=((struct in_addr*)(s_host_info->h_addr))->s_addr; pin1.sin_port=htons(8000); bzero(&pin,sizeof(pin));//用于和client pin.sin_family=AF_INET; pin.sin_addr.s_addr=inet_addr("192.168.157.28"); pin.sin_port=htons(8002); if((socket_s=socket(AF_INET,SOCK_STREAM,0))==-1) //socket_s { perror("failed connect"); exit(1); } if(connect(socket_s,(void*)&pin1,sizeof(pin1))==-1) { perror("failed connect"); exit(1); } if(recv(socket_s,buf,BUF_SIZE,0)==-1)//接受server发来的消息,存放在buf里 { perror("failed recv"); exit(1); } printf("Response form server:/n%s/n",buf); if((socket_s1=socket(AF_INET,SOCK_STREAM,0))==-1)//socket_s1 { perror("failed socket_s1"); exit(1); } if(bind(socket_s1,(struct sockaddr*)&pin,sizeof(pin))==-1) { perror("failed bind"); exit(1); } if(listen(socket_s1,10)==-1) { perror("failed listen"); exit(1); } tmp_socket=accept(socket_s1,(struct sockaddr*)&pin,&add_size); // 接受client的请求 if(tmp_socket==-1) { perror("failed accept"); exit(1); } if(send(tmp_socket,buf,strlen(buf)+1,0)==-1) //发送buf里的数据给client { perror("failed send/n"); exit(1); } bzero(&buf,sizeof(buf)); if(recv(tmp_socket,buf,BUF_SIZE,0)==-1) { perror("failed recv"); exit(1); } printf("received from client:/n%s/n",buf); if(send(socket_s,buf,strlen(buf)+1,0)==-1) { perror("failed send/n"); exit(1); } } client.c #include<stdio.h> #include<sys/socket.h> #include<netinet/in.h> #include<arpa/inet.h> #include<netdb.h> #include<string.h> #include<stdlib.h> #define HOST_NAME "192.168.157.28" #define PORT 8002 #define BUF_SIZE 4096 int main(int argc,char *argv[]) { int socket_descriptor; struct sockaddr_in pin; struct hostent *s_host_info; char buf[8192]; char str[]={"hello,this is a test"}; if((s_host_info=gethostbyname(HOST_NAME))==0) { perror("failed gethostbyname/n"); exit(1); } bzero(&pin,sizeof(pin)); pin.sin_family=AF_INET; pin.sin_addr.s_addr=((struct in_addr *)(s_host_info->h_addr))->s_addr; pin.sin_port=htons(PORT); if((socket_descriptor=socket(AF_INET,SOCK_STREAM,0))==-1) { perror("failed socket"); exit(1); } if(connect(socket_descriptor,(void*)&pin,sizeof(pin))==-1) { perror("failed connect"); exit(1); } printf("sucessful connect with local host/n"); if(send(socket_descriptor,str,strlen(str)+1,0)==-1) { perror("failed send/n"); exit(1); } printf("successful send the message to the server/n"); if(recv(socket_descriptor,buf,BUF_SIZE,0)==-1) { perror("failed recv"); exit(1); } printf("/nResponse form server:/n/n%s/n",buf); close(socket_descriptor); return 0; }