#include<stdio.h> #include<stdlib.h> #include<sys/types.h> #include<sys/socket.h> #include<arpa/inet.h> #include<netinet/in.h> #include<string.h> int start(const char* _ip,const int _port) { int listen_sock=socket(AF_INET,SOCK_STREAM,0); struct sockaddr_in local; if(listen_sock<0){ perror("socket"); exit(1); } local.sin_family=AF_INET; local.sin_port=htons(_port); local.sin_addr.s_addr=inet_addr(_ip); if(bind(listen_sock,(struct sockaddr*)&local,sizeof(local))<0){ perror("bind"); exit(2); } if(listen(listen_sock,5)<0){ perror("listen"); exit(3); } return listen_sock; } void* handler(void* arg) { char buf[1024]; int new_fd=(int)arg; while(1) { ssize_t length=read(new_fd,buf,sizeof(buf)-1); if(length>0) { buf[length]=‘\0‘; printf("client:%s",buf); } else { printf("client close...\n"); exit(0); } } } int main(int argc,char* argv[]) { int listen_sock; int done=0; if(argc!=3){ printf("%s\n",argv[0]); return; } listen_sock=start(argv[1],atoi(argv[2])); while(!done) { struct sockaddr_in client; socklen_t len=sizeof(client); int new_fd=accept(listen_sock,(struct sockaddr*)&client,&len); if(new_fd<0){ perror("accept"); continue; } #ifdef _V1_ char buf[1024]; while(1) { ssize_t length=read(new_fd,buf,sizeof(buf)-1); if(length>0) { buf[length]=‘\0‘; printf("client:%s",buf); } else { printf("client close...\n"); exit(0); } } #elif _V2_ pid_t pid=fork(); if(pid<0) { perror("fork"); exit(0); } else if(pid==0)//child { char buf[1024]; close(listen_sock); while(1) { ssize_t length=read(new_fd,buf,sizeof(buf)-1); if(length>0) { buf[length]=‘\0‘; printf("client:%s",buf); } else { printf("client close...\n"); exit(0); } } close(new_fd); exit(0); } else { close(new_fd); } #elif _V3_ pthread_t tid; if(pthread_create(&tid,NULL,handler,(void*)new_fd)!=0) { perror("pthread_create"); exit(0); } pthread_detach(tid); #else printf("default\n"); #endif } return 0; } #include<stdio.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<arpa/inet.h> #include<string.h> #include<stdlib.h> int main(int argc,char* argv[]) { if(argc!=3) { printf("%s [ip][port]",argv[0]); exit(0); } int socket_client=socket(AF_INET,SOCK_STREAM,0); if(socket_client<0) { perror("socket"); exit(1); } struct sockaddr_in remote; remote.sin_family=AF_INET; remote.sin_port=htons(atoi(argv[2])); remote.sin_addr.s_addr=inet_addr(argv[1]); socklen_t size=sizeof(remote); if(connect(socket_client,(struct sockaddr*)&remote,size)<0) { perror("connect"); exit(2); } char buf[1024]; while(1) { memset(buf,‘\0‘,sizeof(buf)); ssize_t len=read(0,buf,sizeof(buf-1)); if(len>0) { write(socket_client,buf,len); } } return 0; } //双向: #include<stdio.h> #include<stdlib.h> #include<sys/types.h> #include<sys/socket.h> #include<arpa/inet.h> #include<netinet/in.h> #include<string.h> int start(const char* _ip,const int _port) { int listen_sock=socket(AF_INET,SOCK_STREAM,0); struct sockaddr_in local; if(listen_sock<0){ perror("socket"); exit(1); } local.sin_family=AF_INET; local.sin_port=htons(_port); local.sin_addr.s_addr=inet_addr(_ip); if(bind(listen_sock,(struct sockaddr*)&local,sizeof(local))<0){ perror("bind"); exit(2); } if(listen(listen_sock,5)<0){ perror("listen"); exit(3); } return listen_sock; } int main(int argc,char* argv[]) { int listen_sock; int done=0; if(argc!=3){ printf("%s\n",argv[0]); return; } listen_sock=start(argv[1],atoi(argv[2])); while(!done) { struct sockaddr_in client; socklen_t len=sizeof(client); int new_fd=accept(listen_sock,(struct sockaddr*)&client,&len); if(new_fd<0){ perror("accept"); continue; } char buf[1024]; while(1) { ssize_t length=read(new_fd,buf,sizeof(buf)-1); if(length>0) { buf[length]=‘\0‘; printf("client:%s",buf); } else { printf("client close...\n"); exit(0); } length=read(0,buf,sizeof(buf-1)); if(length>0) { buf[length]=‘\0‘; write(new_fd,buf,length+1); } } } return 0; } #include<stdio.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<arpa/inet.h> #include<string.h> #include<stdlib.h> int main(int argc,char* argv[]) { if(argc!=3) { printf("%s [ip][port]",argv[0]); exit(0); } int socket_client=socket(AF_INET,SOCK_STREAM,0); if(socket_client<0) { perror("socket"); exit(1); } struct sockaddr_in remote; remote.sin_family=AF_INET; remote.sin_port=htons(atoi(argv[2])); remote.sin_addr.s_addr=inet_addr(argv[1]); socklen_t size=sizeof(remote); if(connect(socket_client,(struct sockaddr*)&remote,size)<0) { perror("connect"); exit(2); } char buf[1024]; while(1) { ssize_t len; memset(buf,‘\0‘,sizeof(buf)); len=read(0,buf,sizeof(buf-1)); if(len>0) { buf[len]=‘\0‘; write(socket_client,buf,len+1); } len=read(socket_client,buf,sizeof(buf)-1); if(len>0) { buf[len]=‘\0‘; printf("server:%s",buf); } } return 0; }
时间: 2024-10-05 07:32:44