====================================TCPServer.c===================================== #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <string.h> //#include <sys/types.h> #include <netinet/in.h> #include <sys/socket.h> //#include <sys/wait.h>
int main() { int sockfd, new_fd; int sin_size, numbytes; struct sockaddr_in addr, cliaddr; socklen_t addr_len; /******************************************************************* * 建立一个tpcip server 连接, 并取得装置描述m_nSockfd *******************************************************************/ if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror("createSocket"); return -1; } /******************************************************************* * 初始化 sockadd_in 结构 *******************************************************************/ memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons(15800); addr.sin_addr.s_addr = htonl(INADDR_ANY); //绑定套接口 if(bind(sockfd,(struct sockaddr *)&addr,sizeof(struct sockaddr))==-1) { perror("bind"); return -1; } //创建监听套接口 if(listen(sockfd,10)==-1) { perror("listen"); return -1; } char buff[1024]; //等待连接 while(1) { sin_size = sizeof(struct sockaddr_in); perror("server is run"); //如果建立连接,将产生一个全新的套接字 if((new_fd = accept(sockfd,(struct sockaddr *)&cliaddr,&sin_size))==-1) { perror("accept"); return -1; } //生成一个子进程来完成和客户端的会话,父进程继续监听 if(!fork()) { //读取客户端发来的信息 while(1) { memset(buff,0,sizeof(buff)); if((numbytes = recv(new_fd,buff,sizeof(buff),0))==-1) { perror("recv"); return -1; } printf("buff=%s/n",buff); //将从客户端接收到的信息再发回客户端 if(send(new_fd,buff,strlen(buff),0)==-1) perror("send"); } close(new_fd); return 0; } close(new_fd); } close(sockfd); }
================================TCPClient.c========================================= #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <string.h> #include <netdb.h> #include <sys/types.h> #include <netinet/in.h> #include <sys/socket.h>
int main(int argc,char *argv[]) { if(argc!=2) { printf("%s: IPAddress/n",argv[0]); return ; } int sockfd,numbytes; char buf[100]; struct hostent *he; struct sockaddr_in their_addr;int i = 0; //将基本名字和地址转换 he = gethostbyname(argv[1]); //建立一个TCP套接口 if((sockfd = socket(AF_INET,SOCK_STREAM,0))==-1) { perror("socket"); exit(1); } //初始化结构体,连接到服务器的2323端口 their_addr.sin_family = AF_INET; their_addr.sin_port = htons(15800); their_addr.sin_addr = *((struct in_addr *)he->h_addr); bzero(&(their_addr.sin_zero),8); //和服务器建立连接 if(connect(sockfd,(struct sockaddr *)&their_addr,sizeof(struct sockaddr))==-1) { perror("connect"); exit(1); } //向服务器发送字符串 while(1) { scanf("%s",&buf); if(send(sockfd,buf,strlen(buf),0)==-1) { perror("send"); exit(1); } memset(buf,0,sizeof(buf)); //接受从服务器返回的信息 if((numbytes = recv(sockfd,buf,100,0))==-1) { perror("recv"); exit(1); } buf[numbytes] = '/0'; printf("result:%s/n",buf); } close(sockfd); return 0; }
=======================================UDPServer.c==================================== #include <sys/types.h> #include <sys/socket.h> #include <string.h> #include <netinet/in.h> #include <stdio.h> #include <stdlib.h> #include <arpa/inet.h> #include <unistd.h> #define SERV_PORT 15811 #define MAXLINE 100 void dg_echo(int sockfd, struct sockaddr *pcliaddr, socklen_t clilen) { int n; socklen_t len; char mesg[MAXLINE]; for(;;) { len = clilen; memset(mesg,0,sizeof(mesg)); n=recvfrom(sockfd,mesg,MAXLINE,0,pcliaddr,&len); printf("Recv:%s/n",mesg); sendto(sockfd,mesg,n,0,pcliaddr,len); } } int main(int argc, char **argv) { int sockfd; struct sockaddr_in servaddr,cliaddr; sockfd=socket(AF_INET, SOCK_DGRAM, 0); bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(SERV_PORT); bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)); dg_echo(sockfd, (struct sockaddr *)&cliaddr,sizeof(cliaddr)); }
==================================UDPClient.c=================================== #include <sys/types.h> #include <sys/socket.h> #include <string.h> #include <netinet/in.h> #include <stdio.h> #include <stdlib.h> #include <error.h> #define SERV_PORT 15811 #define MAXLINE 100 int main(int argc, char **argv) { int sockfd,so_broadcast; struct sockaddr_in servaddr; if( argc!=2 ) perror("usage:udpclient<IPaddress>"); bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(SERV_PORT); char sendline[MAXLINE]; char recvline[MAXLINE]; sockfd = socket(AF_INET, SOCK_DGRAM, 0); //設置广播属性 if (setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &so_broadcast, sizeof(so_broadcast))) { perror("setsockopt"); return ; } if (sockfd == -1) { perror("socket"); return ; } while(fgets(sendline, MAXLINE,stdin)!=NULL) { //指定发送的IP(可以如192.168.18.255--多播),以下两方法皆可 inet_pton(AF_INET,argv[1],&servaddr.sin_addr); //servaddr.sin_addr.s_addr = inet_addr(argv[1]); sendto(sockfd,sendline,strlen(sendline),0,(struct sockaddr *)&servaddr,sizeof(servaddr)); memset(recvline,0,sizeof(recvline)); int n=recvfrom(sockfd,recvline,MAXLINE,0,NULL,NULL); if(n>0) fputs(recvline,stdout); } exit(0); } |