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\n");
        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\n");
        return -1;
    }
    //listening
    err = listen(ss,BACKLOG);
    if (err < 0)
    {
        printf("listen error\n");
        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\n",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\n");
        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
        }
    }

}

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

posted @ 2015-08-12 13:22  sigma0  阅读(234)  评论(0编辑  收藏  举报