码迷,mamicode.com
首页 > 其他好文 > 详细

实验3

时间:2015-08-30 09:54:43      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:

实验目的:

  • 熟悉基本TCP套接字编程流程(服务器、客户端)
  • 掌握流程中的关键函数的用法

 

实验内容:

客户端:

 1 #include<stdio.h>
 2 #include<unistd.h>
 3 #include<strings.h>
 4 #include<sys/types.h>
 5 #include<sys/socket.h>
 6 #include<netinet/in.h>
 7 #include<netdb.h>
 8 #include<stdlib.h>
 9 #define PORT 1234
10 #define MAXDATASIZE 100
11 int main(int argc, char *argv[])
12 {
13     int sockfd,num;
14     char buf[MAXDATASIZE];
15     struct hostent *he;
16     struct sockaddr_in server;
17     if (argc !=2)
18     {
19         printf("Usage:%s<IP ADDRESS>\n",argv[0]);
20         exit(1);
21     }
22     if((he=gethostbyname(argv[1]))==NULL)
23     {
24         printf("gethostbyname() error\n");
25         exit(1);
26     }
27     if((sockfd=socket(AF_INET, SOCK_STREAM, 0))==-1)
28     {
29         printf("socket() error\n");
30         exit(1);
31     } // 执行socket
32     bzero(&server,sizeof(server));
33     server.sin_family = AF_INET;
34     server.sin_port = htons(PORT);
35     server.sin_addr = *((struct in_addr *)he->h_addr);
36     if(connect(sockfd, (struct sockaddr *)&server,sizeof(server))==-1)
37     {
38         printf("connect() error\n");
39         exit(1);
40     } //执行connect
41     if((num=recv(sockfd,buf,MAXDATASIZE,0)) == -1)
42     {
43         buf[num-1]=\0;
44         exit(1);
45     }
46     buf[num-1]=\0;
47     printf("server message: %s\n",buf);
48     close(sockfd);
49 

 

 

 

 

服务器端:

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<unistd.h>
 4 #include<sys/types.h>
 5 #include<sys/socket.h>
 6 #include<netinet/in.h>
 7 #include<arpa/inet.h>
 8 #include<stdlib.h>
 9 #define PORT 1234
10 #define BACKLOG 1
11 main()
12 {
13     int listenfd, connectfd;
14     struct sockaddr_in server;
15     struct sockaddr_in client;
16     socklen_t addrlen;
17     if((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
18     {
19         perror("socket() error.");
20         exit(0);
21     } // run socket
22     int opt = SO_REUSEADDR;
23     setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
24     bzero(&server, sizeof(server));
25     server.sin_family=AF_INET;
26     server.sin_port=htons(PORT);
27     server.sin_addr.s_addr=htonl (INADDR_ANY);
28     if(bind(listenfd,(struct sockaddr *)&server,sizeof(server)) == -1)
29     {
30         perror("Bind() error");
31         exit(1);
32     } //run bind()
33     if(listen(listenfd,BACKLOG) == -1)
34     {
35         perror("listen() error.\n");
36         exit(1);
37     } // run listen
38     addrlen=sizeof(client);
39     if((connectfd=accept(listenfd,(struct sockaddr *)&client,&addrlen)) ==-1)
40     {
41         perror("accept() error\n");
42         exit(1);
43     } // run accept
44     printf("You get a connection from client‘s ip is %s, port is %d\n",inet_ntoa(client.sin_addr),htons(client.sin_port));
45     send(connectfd,"welcome\n",8,0);
46     close(connectfd);
47     close(listenfd);
48 }

 

实验3

标签:

原文地址:http://www.cnblogs.com/elewei/p/4770316.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!