码迷,mamicode.com
首页 > 系统相关 > 详细

linux socket thread

时间:2018-03-24 17:30:11      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:UNC   turn   create   cti   gcc   centos   fine   file   integer   

/*********************************************************************************
 *      Copyright:  (C) 2018 ligang<2820724771@qq.com>
 *                  All rights reserved.
 *
 *       Filename:  my_socket_thread.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(03/24/2018)
 *         Author:  ligang <2820724771@qq.com>
 *      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;


}

 

编译运行

[ligang@centos6 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
[ligang@centos6 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

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

linux socket thread

标签:UNC   turn   create   cti   gcc   centos   fine   file   integer   

原文地址:https://www.cnblogs.com/lg-blog/p/8639716.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!