TCP / IP Socket Network Programming

Typical Client / Server program flow diagram

Stream-oriented socket program generally follow a predetermined order. For exemplary logic flow diagram of the client and the server, see the figure. In studying this figure, keep in mind the fact that concurrent server usually start before the client starts , and in step 3 waits for a client to request a connection. Then, after closing the client connection, it continues to wait for other client requests.

  • Step 1: server and client use the socket () call creates a stream socket.
  • Step 2 is the client :( optional) using bind () call to bind a socket to a local address s.
  • Step 3: The server uses listen () call to alert willingness to accept the connection to the TCP / IP machine.
  • Step 4: The client uses the connect () call to connect a socket s to an external host.
  • Step 5: The server accepts the connection, and () by blocking accept, until a new connection on this socket for example ns or error.
  • Steps 6 and 7: the server using the send () and the recv () call on the socket ns read and write data, the client read and write data on a socket s, until all data has been exchanged.
  • Step 8: server using close () call to close the socket ns. Client closes socket s, and by the close () call to end TCP / IP session. Go to step 5.

    Basic socket calls

  • Socket: get a read or write to the socket.
  • Bind: The socket is associated with a port number.
  • Listen: Tell TCP/IP The process is listening for connections on this socket.
  • Select: wait for activity on the socket.
  • Accept: accept connections from clients.
  • Connect: allows clients to open a connection to the server port.

    C ++ Network programming: socket implement tcp and udp examples

Server code

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
 
 
int main(int argc, char *argv[])
{
    int server_sockfd;//服务器端套接字
    int client_sockfd;//客户端套接字
    int len;
    struct sockaddr_in my_addr;   //服务器网络地址结构体
    struct sockaddr_in remote_addr; //客户端网络地址结构体
    int sin_size;
    char buf[BUFSIZ];  //数据传送的缓冲区
    memset(&my_addr,0,sizeof(my_addr)); //数据初始化--清零
    my_addr.sin_family=AF_INET; //设置为IP通信
    my_addr.sin_addr.s_addr=INADDR_ANY;//服务器IP地址--允许连接到所有本地地址上
    my_addr.sin_port=htons(8000); //服务器端口号
    
    /*创建服务器端套接字--IPv4协议,面向连接通信,TCP协议*/
    if((server_sockfd=socket(PF_INET,SOCK_STREAM,0))<0)
    {  
        perror("socket error");
        return 1;
    }
 
 
    /*将套接字绑定到服务器的网络地址上*/
    if(bind(server_sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr))<0)
    {
        perror("bind error");
        return 1;
    }
    
    /*监听连接请求--监听队列长度为5*/
    if(listen(server_sockfd,5)<0)
    {
        perror("listen error");
        return 1;
    };
    
    sin_size=sizeof(struct sockaddr_in);
    
    /*等待客户端连接请求到达*/
    if((client_sockfd=accept(server_sockfd,(struct sockaddr *)&remote_addr,&sin_size))<0)
    {
        perror("accept error");
        return 1;
    }
    printf("accept client %s\n",inet_ntoa(remote_addr.sin_addr));
    len=send(client_sockfd,"Welcome to my server\n",21,0);//发送欢迎信息
    
    /*接收客户端的数据并将其发送给客户端--recv返回接收到的字节数,send返回发送的字节数*/
    while((len=recv(client_sockfd,buf,BUFSIZ,0))>0))
    {
        buf[len]='/0';
        printf("%s\n",buf);
        if(send(client_sockfd,buf,len,0)<0)
        {
            perror("write error");
            return 1;
        }
    }
 
 
    /*关闭套接字*/
    close(client_sockfd);
    close(server_sockfd);
    
    return 0;
}

Client Code

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
 
int main(int argc, char *argv[])
{
    int client_sockfd;
    int len;
    struct sockaddr_in remote_addr; //服务器端网络地址结构体
    char buf[BUFSIZ];  //数据传送的缓冲区
    memset(&remote_addr,0,sizeof(remote_addr)); //数据初始化--清零
    remote_addr.sin_family=AF_INET; //设置为IP通信
    remote_addr.sin_addr.s_addr=inet_addr("127.0.0.1");//服务器IP地址
    remote_addr.sin_port=htons(8000); //服务器端口号
    
    /*创建客户端套接字--IPv4协议,面向连接通信,TCP协议*/
    if((client_sockfd=socket(PF_INET,SOCK_STREAM,0))<0)
    {
        perror("socket error");
        return 1;
    }
    
    /*将套接字绑定到服务器的网络地址上*/
    if(connect(client_sockfd,(struct sockaddr *)&remote_addr,sizeof(struct sockaddr))<0)
    {
        perror("connect error");
        return 1;
    }
    printf("connected to server\n");
    len=recv(client_sockfd,buf,BUFSIZ,0);//接收服务器端信息
        buf[len]='/0';
    printf("%s",buf); //打印服务器端信息
    
    /*循环的发送接收信息并打印接收信息(可以按需发送)--recv返回接收到的字节数,send返回发送的字节数*/
    while(1)
    {
        printf("Enter string to send:");
        scanf("%s",buf);
        if(!strcmp(buf,"quit")
            break;
        len=send(client_sockfd,buf,strlen(buf),0);
        len=recv(client_sockfd,buf,BUFSIZ,0);
        buf[len]='/0';
        printf("received:%s\n",buf);
    }
    
    /*关闭套接字*/
    close(client_sockfd);
    
    return 0;
}

Guess you like

Origin www.cnblogs.com/lyszyl/p/12460582.html