Chinaunix首页 | 论坛 | 博客
  • 博客访问: 481330
  • 博文数量: 120
  • 博客积分: 1853
  • 博客等级: 上尉
  • 技术积分: 1177
  • 用 户 组: 普通用户
  • 注册时间: 2011-10-22 22:40
文章分类

全部博文(120)

文章存档

2013年(16)

2012年(104)

分类: LINUX

2013-09-26 23:02:57


点击(此处)折叠或打开

  1. yyp@ubuntu:~$ netstat -anp|grep a.out
  2. (并非所有进程都能被检测到,所有非本用户的进程信息将不会显示,如果想看到所有信息,则必须切换到 root 用户)
  3. tcp 0 0 0.0.0.0:1234 0.0.0.0:* LISTEN 1230/a.out
  4. tcp 0 0 10.2.1.128:44825 10.2.1.128:1234 ESTABLISHED 1231/a.out
  5. tcp 0 0 10.2.1.128:1234 10.2.1.128:44825 ESTABLISHED 1230/a.out

accept会创建一个新的socket,所以服务器+客户端的一次连接会有三个端口,结果如上所示。

下面是client和server的源代码

点击(此处)折叠或打开

  1. #include <iostream>
  2. #include <sys/types.h> /* See NOTES */
  3. #include <sys/socket.h>
  4. #include <netinet/in.h>
  5. #include <unistd.h>
  6. #include <stdint.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. using namespace std;

  10. int main()
  11. {
  12.     int fd;
  13.     fd = socket(AF_INET, SOCK_STREAM, 0);
  14.     if(fd ==-1)
  15.     {
  16.         cout<<"socket error"<<endl;
  17.         exit(-1);
  18.     }
  19.     cout<<"socket ok"<<endl;

  20.     struct sockaddr_in my_addr;
  21.     socklen_t peer_addr_size;

  22.     memset(&my_addr, 0, sizeof(struct sockaddr_in));
  23.    
  24.     my_addr.sin_family=AF_INET;
  25.     my_addr.sin_port=htons(1234);
  26.     my_addr.sin_addr.s_addr=htonl(INADDR_ANY);

  27.     int bindFd = bind(fd, (struct sockaddr*)&my_addr, sizeof(struct sockaddr_in));
  28.     if(bindFd==-1){
  29.         cout<<"bind Error"<<endl;
  30.          close(fd);
  31.      exit(-1);
  32.     }
  33.     cout<<"bind ok"<<endl;
  34.     char buffer[512];
  35.     int liFd = listen(fd, 5);
  36.     if(liFd==-1)
  37.     {
  38.         cout<<"lsten error"<<endl;
  39.         exit(-1);
  40.     }

  41.     cout<<"listening"<<endl;

  42.     while(true)
  43.     {
  44.         cout<<"ready to accept"<<endl;
  45.         struct sockaddr_in peer;
  46.         int acceptFd;
  47.         socklen_t len=sizeof(peer);
  48.         acceptFd = accept(fd,(struct sockaddr *)&peer, &len);
  49.         if(acceptFd == -1)
  50.         {
  51.             cout<<"accept error"<<endl;
  52.          exit(-1);
  53.         }
  54.         cout<<"accepted"<<endl;
  55.         while(true){}
  56.         write(acceptFd, "hello", sizeof("hello"));
  57.         cout<<"write hello"<<endl;
  58.         close(acceptFd);                
  59.     }
  60.     
  61.     
  62.     close(fd);
  63.     
  64.     return 0;
  65. }
client:


点击(此处)折叠或打开

  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <sys/types.h> /* See NOTES */
  4. #include <sys/socket.h>
  5. #include <iostream>
  6. #include <stdlib.h>
  7. #include <netinet/in.h>
  8. #include <arpa/inet.h>
  9. #include <strings.h>
  10. using namespace std;

  11. int main()
  12. {
  13.     int sfd = socket(AF_INET, SOCK_STREAM, 0);
  14.     if(sfd==-1)
  15.     {
  16.         cout<<"socket error"<<endl;
  17.         exit(-1);
  18.     }

  19.     cout<<"socket ok"<<endl;    

  20.     struct sockaddr_in server;
  21.     bzero(&server, sizeof(server));    
  22.     server.sin_family = AF_INET;
  23.     server.sin_port = htons(1234);
  24. //    server.sin_addr.s_addr = inet_addr("10.2.1.128");
  25.     inet_pton(AF_INET, "10.2.1.128", &server.sin_addr);
  26.     cout<<"ready to connect"<<endl;
  27.     printf("%u\n", server.sin_addr.s_addr>>24);
  28.     int cfd = connect(sfd, (struct sockaddr*)&server, sizeof(server));
  29.     if(cfd==-1)
  30.     {
  31.         cout<<"connect error"<<endl;
  32.         exit(-1);
  33.     }

  34.     cout<<"connected"<<endl;
  35.     char buf[1000];
  36.     while(true){}
  37.     read(cfd,buf, 1000);
  38.     cout<<buf<<endl;
  39.     close(cfd);
  40.     close(sfd);
  41.     return 0;
  42. }

阅读(2949) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~