zoukankan      html  css  js  c++  java
  • 网络套接字编程学习笔记一

    创建套接字函数socket:
       1: #include <sys/socket.h> 
       2: int socket (int domain, int type ,int protocol);
       3: // common invoke invoke:
       4: int sockfd = socket(AF_INET, SOCK_STREAM,0);

    //AP_INET IPV4 协议
    //SOCK_STREAM  有序、可靠、双向的面向连接的字节流协议 TCP

    绑定套接字函数bind:

       1: #include <sys/socket.h>
       2: int bind(int sockfd, const char sockaddr *addr,socklen_t len);
     

    成功返回0 ,失败返回 -1;

    处理面向连接的套接字,需要是现在请求服务的进程套接字(客户端)和提供服务的进程套接字(服务器)之间建立一个连接,可以用connect建立一个连接;

       1: #include <sys/socket.h>
       2: int connect(int sockfd, const struct sockaddr *addr , socklen_t len);

    成功返回0,若出错返回-1;

    服务器调用listen来宣告可以接受连接;

       1: #include <sys/socket.h>
       2: int listen(int sockfd ,int backlog);

    成功返回0,出错返回-1;

    一旦服务器调用了listen,套接字就能接受连接请求,服务器使用accept获得连接请求并建立连接

       1: #include <sys/socket.h>
       2: int accept(int sockfd, struct sockaddr *restricted addr, socklen_t* restricted len):

    成功返回文件(套接字)描述符,若出错则返回-1;

    数据传输

    建立连接后可以进行数据传输,数据发送函数send

       1: #include <sys/socket.h>
       2: ssize_t send(int sockfd, const void *buf, size_t nbytes, int flags);

    成功返回发送字节数,失败返回-1;

    接收数据函数recv

       1: #include <sys/socket.h>
       2: ssize_t recv(int sockfd , void* buf,size_t nbytes, int flags);

    返回值,以字节计数的消息长度,若无可用消息或者对方已经按按序结束,则返回0,若出错返回-1;

    面向连接的服务器端程序例子:

       1: /**
       2: ** server.c
       3: ** copyright Xu Dongdong 2013.5.9  Open Source Software License.
       4: */
       5: #include <string.h>
       6: #include <stdio.h>
       7: #include <stdlib.h>
       8: #include <sys/socket.h>
       9: #include <netinet/in.h>
      10: #include <arpa/inet.h>
      11: #include <netdb.h>
      12:  
      13: int port = 8000;
      14: int main (int argc ,char*argv[])
      15: {
      16:     struct sockaddr_in  servaddr;
      17:     struct sockaddr_in pin;
      18:  
      19:     int sockfd;
      20:     int sockfd_tmp;
      21:     int addr_size;
      22:  
      23:     char buf[16384];
      24:     int i,len;
      25:  
      26:     sockfd = socket(AF_INET, SOCK_STREAM,0);
      27:     if(sockfd == -1)
      28:     {
      29:         perror("call to socket error");
      30:         exit(1);
      31:     }
      32:     memset(&servaddr,0,sizeof(servaddr));
      33:     servaddr.sin_family = AF_INET;
      34:     servaddr.sin_addr.s_addr= INADDR_ANY;
      35:     servaddr.sin_port = htons(port);
      36:  
      37:     if(bind(sockfd,(struct sockaddr *)&servaddr ,sizeof(servaddr)) == -1)
      38:     {
      39:         perror("call to bind error");
      40:         _exit(1);
      41:     }
      42:  
      43:     if(listen(sockfd,20) == -1)
      44:     {
      45:         perror("call to listen error");
      46:         exit(1);
      47:     }
      48:  
      49:     printf("Accepting connections ......\n");
      50:  
      51:     while(1)
      52:     {
      53:         sockfd_tmp = accept(sockfd,(struct sockaddr*)&pin,&addr_size);
      54:         if(sockfd_tmp == -1)
      55:         {
      56:               perror("call to accept error");
      57:               exit(1);
      58:         }
      59:  
      60:         if(recv(sockfd_tmp,buf,16384,0) == -1)
      61:         {
      62:               perror("call to recv error");
      63:               exit(1);
      64:         }
      65:         printf("receive fronm client:%s\n",buf);
      66:  
      67:         len = strlen(buf);
      68:         for(i=0;i<len;i++)
      69:         {
      70:             buf[i]= toupper(buf[i]);
      71:         }
      72:  
      73:         if(send(sockfd_tmp ,buf,len ,0) == -1)
      74:         {
      75:               perror("call to send error");
      76:         exit(1);
      77:         }
      78:         close(sockfd_tmp);
      79:     }
      80: }

    面向连接的客户端进程:

       1: /**
       2: ** client.c
       3: ** copyright Xu Dongdong 2013.5.9  Open Source Software License.
       4: */
       5: /*#include <unistd.h> */
       6: #include <string.h> /*memset */
       7: #include <stdio.h>
       8: #include <stdlib.h> /*exit*/
       9: #include <sys/socket.h>
      10: #include <netinet/in.h>
      11: #include <arpa/inet.h>
      12: #include <netdb.h>
      13:  
      14: char * host_name = "127.0.0.1";
      15: int port = 8000 ;
      16:  
      17: int main (int argc ,char*argv[])
      18: {
      19:     char buf[8192];
      20:     char messge[256];
      21:    
      22:     struct sockaddr_in pin;
      23:  
      24:     int sockfd,listenfd ;
      25:  
      26:     struct hostent *server_host_name;
      27:     char *str ="A default test string ";
      28:  
      29:     if(argc <2)
      30:     {
      31:         printf("Using :  test  \"Any test string\"   \n");
      32:         printf("We will send a default ttest sting   \n");
      33:     }else{
      34:         str = argv[1];
      35:     }
      36:  
      37:     if((server_host_name = gethostbyname(host_name)) ==0)
      38:     {
      39:         perror("Error resolving local host \n");
      40:         exit(1);
      41:     }
      42:  
      43:  
      44:     sockfd = socket(AF_INET, SOCK_STREAM,0);
      45:     if(sockfd == -1)
      46:     {
      47:         perror("call to socket error");
      48:         exit(1);
      49:     }
      50:  
      51:     memset(&pin,'\0',sizeof(pin));
      52:     pin.sin_family = AF_INET;
      53:     pin.sin_addr.s_addr= htonl(INADDR_ANY);
      54:     pin.sin_addr.s_addr = 
      55: (( (struct in_addr *)(server_host_name -> h_addr)) -> s_addr);
      56:     pin.sin_port = htons(port);
      57:  
      58:     if(connect(sockfd,(struct sockaddr *)&pin ,sizeof(pin)) == -1)
      59:     {
      60:         perror("call to connet to socket ");
      61:         exit(1);
      62:     }
      63:  
      64:     printf("Send message  %s to server \n",str);
      65:  
      66:     if(send(sockfd,str,strlen(str), 0) == -1)
      67:     {
      68:               perror("call to send error");
      69:               exit(1);
      70:     }
      71:  
      72:         printf("...send message...wait for response \n");
      73:  
      74:     if(recv(sockfd,buf,8192,0) == -1)
      75:     {
      76:               perror("error in receiving response from server ");
      77:               exit(1);
      78:     }
      79:         printf("Respnse fronm server:%s\n",buf);
      80:  
      81:         close(sockfd);
      82: }

    使用gcc编译程序,并运行即可,先运行服务器程序,后运行客户端程序

  • 相关阅读:
    Laravel5 cookie和session设置
    php如何实现登陆后返回原页面
    laravel5项目安装debugbar
    phpstorm安装laravel-ide-helper实现自动完成、代码提示和跟踪
    js 3秒后跳转页面的实现代码
    phpstorm常用plugins
    运用Xdebug调试和优化PHP程序
    Laravel5 打印SQL
    laravel 去掉index.php伪静态
    Linux gprof命令
  • 原文地址:https://www.cnblogs.com/xuddong/p/3070914.html
Copyright © 2011-2022 走看看