linux socket thread

Posted 李刚blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux socket thread相关的知识,希望对你有一定的参考价值。

/*********************************************************************************
 *      Copyright:  (C) 2018 ligang<[email protected]>
 *                  All rights reserved.
 *
 *       Filename:  my_socket_thread.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(03/24/2018)
 *         Author:  ligang <[email protected]>
 *      ChangeLog:  1, Release initial version on "03/24/2018 03:07:58 PM"
 *                 
 ********************************************************************************/
#include<string.h>
#include<stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h> //?á11ì?sockaddr?ùDèí·???t
#include<errno.h>
#include <pthread.h>

#define PORT 8555
void *thread_worker(void * arg);
int main(int argc,char **argv)
{
    int serv_fd;
    struct sockaddr_in serv_addr;

    serv_fd = socket(AF_INET,SOCK_STREAM,0);
    if(serv_fd == -1)
    {
        printf("socket is failure :%s\n",strerror(errno));
        return -1;
    }

    memset(&serv_addr,0,sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(PORT);
    serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);//×??ˉ??è?±??úipμ??·
    printf("listen port is %d\n",PORT);

    if(bind(serv_fd,(const struct sockaddr*)&serv_addr,sizeof(serv_addr))==-1)
    {
        printf("bind is failure :%s\n",strerror(errno));
        return -1;
    }

    if(listen(serv_fd,5)==-1)
    {
        printf("listen is failure :%s\n",strerror(errno));
        return -1;
    }

    while(1)
    {
        pthread_t tid;
        int new_fd;
        printf("    begin to accept!!!    \n");
        new_fd = accept(serv_fd,NULL,NULL);
        if(new_fd<0)
        {
            printf("accept is fail :%s",strerror(errno));
            return -2;
        }

        pthread_create(&tid,NULL,thread_worker,(void*)new_fd);
         //close(new_fd)不能写,否则客户端,服务器端不能收到信息
    }
    close(serv_fd);
}


void *thread_worker(void * arg)
{
    char buf[1024];
    int fd = (int)arg;
   
    printf("fd is[%d]\n",fd);
    memset(buf,0,sizeof(buf));
    read(fd,buf,sizeof(buf));
    printf("read %s from client\n",buf);

    write(fd,"welcome",7);
    sleep(1);
    close(fd);
    return NULL;


}
#include<string.h>
#include<stdio.h>
#include <sys/types.h> 
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include<errno.h>

int main(int argc,char **argv)
{
        int conn_fd;
        struct sockaddr_in serv_addr;
        char buf[1024];

        conn_fd=socket(AF_INET,SOCK_STREAM,0);
        if(conn_fd<0)
        {
                printf("create socket is failure %s\n",strerror(errno));
                return -1;
        }
        printf("create socket number [%d]\n",conn_fd);

        memset(&serv_addr,0,sizeof(serv_addr));
        serv_addr.sin_family = AF_INET;
        serv_addr.sin_port = htons(8555);
        inet_aton("192.168.0.5",&serv_addr.sin_addr);

        printf("begin to connecting\n");

        if(connect(conn_fd,( struct sockaddr *)&serv_addr,sizeof(serv_addr))<0)
        {
          printf("connect is failuren:  %s\n",strerror(errno));
          return -2;
        }

        printf("connect is sucess\n");

        write(conn_fd,"hello",5);

        memset(buf,0,sizeof(buf));
        read(conn_fd,buf,sizeof(buf));
        printf("receive : %s \n",buf);

        close(conn_fd);
        return 0;


}

 

编译运行

[[email protected] socket]$ gcc my_socket_thread.c  -lpthread
my_socket_thread.c: In function ‘main’:
my_socket_thread.c:66: warning: cast to pointer from integer of different size
my_socket_thread.c: In function ‘thread_worker’:
my_socket_thread.c:76: warning: cast from pointer to integer of different size
[[email protected] socket]$ ./a.out
listen port is 8555
    begin to accept!!!   
    begin to accept!!!   
fd is[4]
read hello from client
    begin to accept!!!   
fd is[5]
read hello from client
^C

[[email protected] socket]$ ./a.out
create socket number [3]
begin to connecting
connect is sucess
receive : welcome
[[email protected] socket]$ gcc my_socket_client.c
[[email protected] socket]$ ./a.out
create socket number [3]
begin to connecting
connect is sucess
receive : welcome 

























以上是关于linux socket thread的主要内容,如果未能解决你的问题,请参考以下文章

Python3 socket 实现即时通讯脚本,threading 多线程

Python3 socket 实现即时通讯脚本,threading 多线程

NDK: ant 错误 [javah] Exception in thread "main" java.lang.NullPointerException 多种解决办法(代码片段

Socket 多任务

多线程 Thread 线程同步 synchronized

1socket+threading编程之TCP