zoukankan      html  css  js  c++  java
  • linux 服务器/客户端 tcp通信的简单例子

    昨天弄了sublime之后没有弄输入中文的了,学生党来着,刚好可以练练英语(英语渣渣,还要考六级),所以注释都写英文的(语法什么的就别太深究了)
    服务器端:

    /*start from the very beginning,and create greatness
    @name:Chuangwei Lin
    @E-mail:979951191@qq.com
    @brief:a simple example about server/client link(server c file)
    */
    #include <stdio.h>
    #include <stdlib.h>
    #include <strings.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <unistd.h>
    #include <linux/in.h>
    #define PORT 8888 //port to listen
    #define BACKLOG 2 //the length of listen queue
    void process_conn_server(int s);
    int main(int argc, char const *argv[])
    {
        int ss,sc; //ss:server's socket file descriptor;sc:client's socket file descriptor
        struct sockaddr_in server_addr; //server's address struct
        struct sockaddr_in client_addr; //use to save client's address 
        int err; //the return value
        pid_t pid; //the process ID
        //create a socket
        ss = socket(AF_INET,SOCK_STREAM,0);
        if (ss < 0) //error
        {
            printf("socket error
    ");
            return -1;
        }
        //set the server address
        bzero(&server_addr,sizeof(server_addr)); //clear
        server_addr.sin_family = AF_INET; //protocol family
        server_addr.sin_addr.s_addr = htonl(INADDR_ANY);//local address
        server_addr.sin_port = htons(PORT); //server port
        //binging 
        err = bind(ss,(struct sockaddr*)&server_addr,sizeof(server_addr));
        if (err < 0)
        {
            printf("bind error
    ");
            return -1;
        }
        //listening
        err = listen(ss,BACKLOG);
        if (err < 0)
        {
            printf("listen error
    ");
            return -1;
        }
        //the main loop
        for (;;) 
        {
            int addrlen = sizeof (struct sockaddr);
            sc = accept(ss,(struct sockaddr*)&client_addr,&addrlen); //accept the client's connecting
            if (sc < 0)
            {
                continue; //if error,finish this time loop
            }
            //create a new process to process the connecting
            pid = fork();
            if (0 == pid)
            {
                close(ss); //close listen in the child process
                process_conn_server(sc); // process 
            }
            else
            {
                close(sc); //close accept in the parent process
            }
    
        }
    
        return 0;
    }
    //the process function
    void process_conn_server(int s)
    {
        ssize_t size = 0 ;
        char buffer[1024]; //data buffer
        for(;;)
        {
            size = read(s,buffer,1024); //read data from socket and put in buffer
            if (0 ==size) //if no data
            {
                return; //return
            }
            sprintf(buffer,"%d bytes altogether
    ",size); //send the number of data return
            write(s,buffer,strlen(buffer)+1);
        }
    
    }

    客户端:

    /*start from the very beginning,and create greatness
    @name:Chuangwei Lin
    @E-mail:979951191@qq.com
    @brief:a simple example about server/client link(client c file)
    */
    #include <stdio.h>
    #include <stdlib.h>
    #include <strings.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <unistd.h>
    #include <linux/in.h>
    #define PORT 8888 //port to listen
    void process_conn_client(int s);
    int main(int argc, char const *argv[])
    {
        int s; //s:socket descriptor
        struct sockaddr_in server_addr; // server address
        int err;
        //create a socket
        s = socket(AF_INET,SOCK_STREAM,0);
        if (s < 0)
        {
            printf("socket error
    ");
            return -1;
        }
        //set the server address
        bzero(&server_addr,sizeof(server_addr));
        server_addr.sin_family = AF_INET; //protocol family
        server_addr.sin_addr.s_addr = htonl(INADDR_ANY); //local address
        server_addr.sin_port = htons(PORT); //server port
    
        //change the user input char IP to int 
        inet_pton(AF_INET,argv[1],&server_addr.sin_addr);
        //conneting the server 
        connect(s,(struct sockaddr*)&server_addr,sizeof(struct sockaddr));
        process_conn_client(s);
        close(s);
        return 0;
    }
    void process_conn_client(int s)
    {
        ssize_t size = 0 ;
        char buffer[1024]; //data buffer
        for(;;)
        {
            //get data from standard input
            size = read (0,buffer,1024);
            if(size > 0)
            {
                write(s,buffer,size); //send to server
                size = read(s,buffer,1024); //read from server
                write(1,buffer,size); //write to standard output
            }
        }
    
    }

    很简单的一段,只是建立连接然后客户端发送字符然后服务器统计后返回

  • 相关阅读:
    java 数组声明方法
    python 什么叫迭代
    Golang生成区间随机整数
    Golang字符串格式化
    Golang中map的三种声明方式和简单实现增删改查
    Golang实现二分查找法
    Golang实现冒泡排序法
    Golang切片的三种简单使用方式及区别
    Golang获取int数组里的最大值和下标
    Golang数组注意细节
  • 原文地址:https://www.cnblogs.com/sigma0-/p/12630532.html
Copyright © 2011-2022 走看看