linux 网络编程---->简单实例—转

当然此处仅仅是对于单用户请求的处理,对于多用户的请求后面再说!

对于server处理:
            server要做的就是创建自己的套接字,然后设置一些参数:例如协议类型,IP之类。
            然后就是bind参数,再次就是监听( listen),最后就是accept进入睡眠等待状态,if
            有cilent的请求就会有反应!
      
对于chient的处理:
            建立自己的套接字,然后就是连接到server,发送自己的数据,然后等待server的回送!


server.cpp

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #include<errno.h>
  4 #include<string.h>
  5 #include<sys/types.h>
  6 #include<sys/wait.h>
  7   
  8 
  9 #include<sys/socket.h>
 10 #include<netinet/in.h>
 11 #include<arpa/inet.h>
 12  
 13 #include <iostream>
 14 using namespace std;
 15  
 16 #define PORT6000                  //!> 端口
 17 #define MAX_REQ5               //!> 监听情况下:在请求队列中允许的最大请求数
 18  
 19 int main()
 20 {
 21     intlisten_socket,client_socket;         //!> 监听,客户套接字
 22     structsockaddr_inlocal_addr;         //!> server结构
 23     structsockaddr_inclient_addr;      //!> client结构
 24     intrecv_len;
 25     
 26 
 27  
 28     charbuf[1024];
 29     charrecv_buf[1024];
 30  
 31    //!>    intsocket(int domain, int type, int protocol);参数:协议族;套接字类型;协议,一般为0->TCP/IP
 32  
 33    //!> 创建套接字
 34    //!>
 35    if((listen_socket = socket(AF_INET,SOCK_STREAM, 0)) ==-1)         //!> 创建套接字
 36     {
 37        cout << "创建server套接字失败~"<< endl;
 38        exit(1);
 39     }
 40  
 41    //!> 下面设置本地的套接字的参数结构
 42    //!> 
 43    local_addr.sin_family =AF_INET;                              //!> 代表TCP/IP
 44    local_addr.sin_port =htons(PORT);                           //!> 端口号( 转化成流格式 )
 45                                                             //!> if此处写上0,那么系统随机选择一个未被使用的端口号
 46    local_addr.sin_addr.s_addr =INADDR_ANY;                     //!> 填入本机IP地址( INADDR_ANY )
 47    bzero(&(local_addr.sin_zero),8);                              //!> 填充0保持大小一致
 48  
 49  
 50    //!> 下面绑定到套接字
 51    //!> 
 52    if(bind(listen_socket, (structsockaddr*)&local_addr, sizeof(struct sockaddr)) ==-1)      //!> 将设置好的参数绑定到套接字
 53     {
 54        cout << "server套接字绑定参数失败~"<< endl;
 55        exit(1);
 56     }
 57     else
 58     {
 59        cout << "绑定成功!"<< endl;
 60     }
 61  
 62    //!> 下面开始监听
 63    //!>为该socket建立一个输入数据队列,将到达的服务请求保存在此队列中,直到程序处理它们
 64    //!> 
 65    if(listen(listen_socket, MAX_REQ) ==-1)                
 66     {
 67        cout << "监听失败!"<< endl;
 68        exit(1);
 69     }
 70     else
 71     {
 72        cout << "server开始监听!"<< endl;
 73     }
 74     
 75     int len =sizeof(struct sockaddr_in);
 76     
 77     while( 1)
 78    { 
 79       //!> 建立好输入队列后,服务器就调用accept函数,然后睡眠并等待客户的连接请求
 80       //!>accept指出了在哪个套接字口进行监听;获取发送请求的client的套接字地址,为了回送;第三个参数是套接字地址的整形指针
 81       if((client_socket = accept(listen_socket, (structsockaddr*)&client_addr,(socklen_t*)&(len ))) == -1)
 82        {
 83            cout<< "server等待请求错误!"<< endl;
 84              exit(1);
 85        }
 86        else
 87        {
 88            cout<< "server正在等待请求!"<< endl;
 89        }
 90     
 91       //!> 收到请求
 92        cout<<"收到请求来自IP地址:"<<inet_ntoa(client_addr.sin_addr) <<"端口号:" << ntohs(client_addr.sin_port)<< endl;
 93         
 94        
 95         cout<< "准备发送数据:"<<endl;  
 96         
 97        strcpy(buf,"");
 98           while( 1)
 99           {
100           cin >>buf;                                       //!> 输入请求
101           int sendlen =strlen(buf);                            
102            
103           if((send(client_socket, buf, sendlen, 0)) ==-1)         //!> 发送回去
104           {
105               cout << "发送错误!"<< endl;
106           }
107            
108           if( strcmp( buf, "quit" ) ==0 || strcmp( buf, "q" ) ==0 || strcmp(buf, "exit" ) ==0 )
109           {
110                  cout<< "发送成功!"<< endl;
111                  break;
112           }
113           }
114     }
115     
116     close(listen_socket);                                    //!> 关闭套接字
117 }

client.cpp

 

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 #include<errno.h>
 5 #include<sys/wait.h>
 6 #include<sys/socket.h>
 7 #include<sys/types.h>
 8 #include<netinet/in.h>
 9 #include<arpa/inet.h>
10 #include <iostream>
11 using namespace std;
12 #define PORT 6000
13  
14 int main(  int argc, char **argv  )
15 {
16     intclient_socket;
17     structsockaddr_in serv_addr;
18  
19     charbuf[1024];
20     intrecv_len;
21  
22     if( argc !=2 )
23     {
24        cout<< "请输入请求信息参数!"<< endl;
25     }
26  
27    //!> 建立套接字
28    //!> 
29    if((client_socket = socket(AF_INET, SOCK_STREAM, 0)) ==-1)         //!> 建立套接字
30     {
31        cout << "client创建套接字失败~"<< endl;
32        exit(1);
33     }
34  
35    //!>下面就是server的套接字参数设,为了访问的需要,所以server的套接字必须是公开的信息
36    //!> 
37    serv_addr.sin_family = AF_INET;
38    serv_addr.sin_port = htons(PORT);
39    serv_addr.sin_addr.s_addr =inet_addr("10.80.0.195");            //!> server 的 IP地址
40    bzero(&(serv_addr.sin_zero), 8);
41  
42    //!> 连接到server
43    if((connect(client_socket, (structsockaddr*)&serv_addr, sizeof(struct sockaddr))) ==-1)
44     {
45        cout << "client连接sever失败~"<< endl;
46        exit(1);
47     }
48  
49     cout<< "client发送数据:"<< argv[1]<< endl;
50     
51    //!> 下面就可以接受数据了
52    
53    strcpy( argv[1], buf);                     //!> 命令行参数就是请求信息
54    
55      if((send(client_socket, buf, strlen(buf), 0)) ==-1)         //!> 发送回去
56     {
57         cout<< "发送错误!"<< endl;
58     }
59    
60       cout<< "client接受数据:"<< endl;
61    
62    while(1)
63     {
64        if((recv_len = recv(client_socket, buf, sizeof(buf), 0)) ==-1)      //!> 接收数据
65        {
66            cout << "接收错误! "<< endl;
67            exit(1);
68        }
69  
70        buf[recv_len] = '\0';
71           
72           if( strcmp(buf, "quit" ) ==0 || strcmp( buf, "q" ) ==0 || strcmp( buf, "exit") ==0  )
73           {
74              cout<< "recv finish..."<< endl;
75              exit( 0);
76           }
77       
78           cout<< "接收到:"<< buf<< endl;
79  
80     }
81  
82    close(client_socket);
83 }
84 
85 
86 makefile:
87 
88 do: server client
89 
90 server:
91     g++ -w-o  server server.cpp
92 
93 client:
94     g++ -w-o  client  client.cpp
95    
96 clean:
97     rm -f serverclient   

 

 

 

posted @ 2012-07-27 15:15  戊辰岁终  阅读(142)  评论(0编辑  收藏  举报