首先,可以将代码复制下来放到U盘里,然后挂载到Linux上
挂载步骤
找到设备->USB->你U盘的名字
挂载成功
访问U盘把代码拷贝到home文件夹下,就可以直接进行编译。
client.c
#include <stdio.h> #include <unistd.h>#include <strings.h>#include<string.h>#include <sys/types.h> #include <sys/socket.h> #include <stdlib.h>#include <netinet/in.h> #include <netdb.h> #define PORT 1234 #define MAXDATASIZE 100 char receiveM[100];char sendM[100]; int main(int argc, char *argv[]) { int fd, numbytes; struct hostent *he; struct sockaddr_in server; //检查用户输入,如果用户输入不正确,提示用户正确的输入方法if (argc !=2) { printf("Usage: %s <IP Address>\n",argv[0]); exit(1); } // 通过函数 gethostbyname()获得字符串形式的ip地址,并赋给heif ((he=gethostbyname(argv[1]))==NULL){ printf("gethostbyname() error\n"); exit(1); } // 产生套接字fdif ((fd=socket(AF_INET, SOCK_STREAM, 0))==-1){ printf("socket() error\n"); exit(1); } bzero(&server,sizeof(server));server.sin_family = AF_INET; server.sin_port = htons(PORT); server.sin_addr = *((struct in_addr *)he->h_addr); if(connect(fd, (struct sockaddr *)&server,sizeof(struct sockaddr))==-1){ printf("connect() error\n"); exit(1); } // 向服务器发送数据 printf("send message to server:");fgets(sendM,100,stdin);int send_le;send_le=strlen(sendM);sendM[send_le-1]='\0';send(fd,sendM,strlen(sendM),0); // 从服务器接收数据if ((numbytes=recv(fd,receiveM,MAXDATASIZE,0)) == -1){ printf("recv() error\n"); exit(1); } printf("receive message from server:%s\n",receiveM);close(fd); }
server.c
#include <stdio.h> #include <string.h> #include <strings.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h>#include<pthread.h>#define PORT 1234 #define BACKLOG 1 void *start_routine( void *ptr) {int fd = *(int *)ptr;char buf[100];int numbytes;int i,c=0;printf("this is a new thread,you got connected\n");printf("fd=%d\n",fd);if ((numbytes=recv(fd,buf,100,0)) == -1){ printf("recv() error\n"); exit(1); } char str[numbytes];char buffer[numbytes];//将收到的字符串反转for(c=0;c<(numbytes-1);c++){buffer[c]=buf[c];}printf("receive message:%s\n",buf);printf("numbytes=%d\n",numbytes);for(i=0;i<numbytes;i++){str[i]=buf[numbytes-1-i];}printf("server will send:%s\n",str);numbytes=send(fd,str,sizeof(str),0); printf("send numbytes=%d\n",numbytes);close(fd); }int main() { int listenfd, connectfd; struct sockaddr_in server; struct sockaddr_in client; int sin_size; sin_size=sizeof(struct sockaddr_in); pthread_t thread; //定义一个线程号if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("Creating socket failed.");exit(1);}int opt = SO_REUSEADDR;setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));bzero(&server,sizeof(server)); server.sin_family=AF_INET; server.sin_port=htons(PORT); server.sin_addr.s_addr = htonl (INADDR_ANY); // 绑定if (bind(listenfd, (struct sockaddr *)&server, sizeof(struct sockaddr)) == -1) { perror("Bind error.");exit(1); } // 监听 if(listen(listenfd,BACKLOG) == -1){ /* calls listen() */ perror("listen() error\n"); exit(1); } while(1){// accept if ((connectfd = accept(listenfd,(struct sockaddr *)&client,&sin_size))==-1) {perror("accept() error\n"); exit(1); }printf("You got a connection from %s\n",inet_ntoa(client.sin_addr) ); pthread_create(&thread,NULL,start_routine,(void *)&connectfd);}close(listenfd);}
代码从U盘复制到home后,编译执行
打开三个中端
可以发现用户端和服务器端连接成功,即用户端发送的信息,服务器端可以接收的到。