Simple client-server simulation projection

The following simulated a simple client-server projection simulation process. The client sends data as a server, the server receives the data sent back to the client. And then printed out by the client.

Required function:

Networking

server

socket(AF_INET,SOCK_STREAM,0);  AF_INET表示IPV4,SOCK_STREAM表示基于字节流的,0表示协议由前面两个参数组合而成。返回描述符

bind(sockdf,(struct sockaddr*)servaddr,sizeof(servaddr));   用于把描述符与本地协议地址联系起来。

listen(listenfd,1024);   监听队列,最多监听1024个连接

accept(listenfd,(struct sockaddr *)&cliaddr,&clilen); 从完成队列里面取出套接字进行链接,如果没有,则睡眠等待。

close(connfd);   关闭连接

Client computer

sockfd = socket(AF_INET,SOCK_STREAM,0); 与上面相同

connect(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr)); 向服务器发起连接,三次握手是在这里触发

IO aspects

fgets(sendline,MAXLINE,stdin);   从标准输入中获取数据写入到sendline中,回车结束

fputs(recvline,stdout);   从缓冲区recvline中读取数据写入到stdout中

read(sockfd,recvline,MAXLINE);   从sockfd中读入数据到recvline中

write(sockfd,sendline,strlen(sendline));   向sockfd中写入sendline中的数据

Server-side code:

#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <strings.h>
#define SERV_PORT 9877
#define LISTENQ 1024
#define MAXLINE 1024

int main(int argc,char ** argv){
    int            listenfd,connfd;
    pid_t        childpid;
    socklen_t    clilen;
    char         buf[MAXLINE];
    int         n;
    struct sockaddr_in    cliaddr,servaddr;
    listenfd = socket(AF_INET,SOCK_STREAM,0);
    
    bzero(&servaddr,sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servaddr.sin_port = htons(SERV_PORT);

    bind(listenfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
    listen(listenfd,LISTENQ);

    for( ; ; ){
        clilen = sizeof(cliaddr);
        connfd = accept(listenfd,(struct sockaddr *)&cliaddr,&clilen);
        if((childpid = fork()) == 0){
            close(listenfd);
            while((n=read(connfd,buf,MAXLINE)) > 0){
                printf("from client:%s",buf);
                write(connfd,buf,n);
                int i=0;
                for(i=0;i<MAXLINE;i++){
                    buf[i] = '\0';
                }
            }
            exit(0);
        }
        close(connfd);
    }
}

The client-side code:

#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <strings.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#define MAXLINE 1024
int main(int argc, char const *argv[])
{
    int                    sockfd;
    struct     sockaddr_in    servaddr;

    char sendline[MAXLINE],recvline[MAXLINE];

    if(argc != 2)
        printf("error! to clien IP\n");

    sockfd = socket(AF_INET,SOCK_STREAM,0);
    bzero(&servaddr,sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(9877);
    inet_pton(AF_INET,argv[1],&servaddr.sin_addr);

    connect(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr));

    while(fgets(sendline,MAXLINE,stdin) != NULL){
        write(sockfd,sendline,strlen(sendline));
        //fflush(stdin);
        read(sockfd,recvline,MAXLINE);
        //printf("from server:%s\n", recvline);
        fputs(recvline,stdout);
        int i;
        for(i=0;i<MAXLINE;i++){
            recvline[i] = '\0';
            sendline[i] = '\0';
        }

    }
    exit(0);
}

Because each time data is sent, the server has buf, as well as the client side sendline recvline last of the old data, so every once assigned '\ 0', to ensure that the last data will not have an impact on this.

operation result

Running in a terminal:

gcc -Wall -o serv serv.c

./serv

The other open end run:

gcc -Wall -o clien clie.c

./clie 127.0.0.1

At this time, the transmission data at the client side

[xingoo@localhost tcpip]$ ./clie 127.0.0.1
ffffff
ffffff
dd
dd
gg
gg
hello
hello
heiheieieihehi
heiheieieihehi
fdafdasfdasfdasfdasfdas
fdafdasfdasfdasfdasfdas
d
d

Server receives

[xingoo@localhost tcpip]$ ./serv
from client:ffffff
from client:dd
from client:gg
from client:hello
from client:heiheieieihehi
from client:fdafdasfdasfdasfdasfdas
from client:d

 

Reproduced in: https: //my.oschina.net/u/204616/blog/545429

Guess you like

Origin blog.csdn.net/weixin_34205076/article/details/91989413