多线程socket编程示例

socket编程的基础函数使用请自查询,本文只给出在linux gcc可编译运行的socket通信的服务器程序与客户端程序。
代码如下:server端

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>                //signal
#include <sys/socket.h>            //socket
#include <netinet/in.h>         //sockaddr_in
#include <arpa/inet.h>
#include <string.h>                //memset
const int BUFF_SIZE=10;
void process(int client_fd,int cmd)
{
    switch(cmd)
    {
    case 0:
        printf("rec cmd 0");break;
    case 1:
    default:
        printf("default %d\n",cmd);break;
    }
    send(client_fd,"OK",BUFF_SIZE,0);
}
int main(void) {
    int server_fd,client_fd;    //socket fd
    struct sockaddr_in my_addr;   //服务器网络地址结构体
    struct sockaddr_in remote_addr; //客户端网络地址结构体
    int cmd;
    char str[10];
    pid_t pid;
    unsigned int len;
//1.init
    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);         //服务器端口号
//2.socket
    if( (server_fd = socket(AF_INET,SOCK_STREAM,0)) < 0)//create socket;
    {
        perror("socket create failed");
        exit(-1);
    }
//3.bind
    if (bind(server_fd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr))<0)
    {
        perror("bind failed");
        return 1;
    }
//4.listen
    listen(server_fd,5);
    puts("Now server begin listening ,the listen queue len is5");


    signal(SIGCLD,SIG_IGN);     //ignore teh detail of the child process exit

    int client_len = sizeof(remote_addr);
    while(1){
//5.accept
        if((client_fd=accept(server_fd,(struct sockaddr *)&remote_addr,&client_len))<0)
            {
                perror("accept");
                return 1;
            }
        if( (pid = fork())== 0)            //child
        {
    //        client_fd= accept(server_fd,(struct sockaddr*)&remote_addr,&client_len);
    //        puts("a client connected");
//6.receive
            printf("working for %s\n",inet_ntoa(remote_addr.sin_addr));
            len = send(client_fd,"welcome!",BUFF_SIZE,0);
            puts("send welcome!");
            len = recv(client_fd,&cmd,sizeof(cmd),0);
            process(client_fd,cmd);
            close(client_fd);
            exit(0);
        }
        if(pid)                    //parent
        {
            puts("In parent process");
            close(client_fd);
        //    puts("parent process over");
            sleep(1);
        }
    }
    close(server_fd);
    return EXIT_SUCCESS;
}



#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>                //signal
#include <sys/socket.h>            //socket
#include <netinet/in.h>         //sockaddr_in
#include <arpa/inet.h>
#include <string.h>                //memset
const int BUFF_SIZE=10;
int main(void) {
        int client_sockfd;
        int len,cmd;
        struct sockaddr_in remote_addr; //服务器端网络地址结构体
        char buf[BUFF_SIZE];  //数据传送的缓冲区
        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); //服务器端口号

       
        if((client_sockfd=socket(PF_INET,SOCK_STREAM,0))<0)
        {
            perror("socket");
            return 1;
        }

       
        if(connect(client_sockfd,(struct sockaddr *)&remote_addr,sizeof(struct sockaddr))<0)
        {
            perror("connect");
            return 1;
        }
        printf("connected to server\n");
        len=recv(client_sockfd,buf,BUFSIZ,0);//接收服务器端信息
             buf[len]=‘/0‘;
        printf("received %s\n",buf); //打印服务器端信息

       
        puts("please input cmd");
        scanf("%d",&cmd);
            len=send(client_sockfd,&cmd,4,0);
            len=recv(client_sockfd,buf,BUFF_SIZE,0);
            buf[len]=‘/0‘;
                printf("received %s\n",buf); //打印服务器端信息

        close(client_sockfd);//关闭套接字
        return 0;
}



多线程socket编程示例,布布扣,bubuko.com

多线程socket编程示例

上一篇:vue-通过设置CSS,鼠标放上面后显示成手指;


下一篇:数据结构栈C++简单实现