打印

linux 下 TCP/IP Socket 编程

[复制链接]
260|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
冷冻链|  楼主 | 2018-10-5 19:46 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include "socket.h"













int thread_count = 0;

pthread_t threads[MAX_THREAD_NUM];



/**

* 接收数据的执行函数

* @param conn_socket

*/

void recv_data(int conn_socket);



int Socket_Listent()

{

// 建立Socket

int server_socket = socket(PF_INET, SOCK_STREAM, 0);

if (server_socket < 0) {

printf("Socket create failed.\n");

return EXIT_FAILURE;

}



{

int opt = 1;

setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof (opt));

}



// 设置服务器端监听套接字参数

struct sockaddr_in server_addr;

bzero(&server_addr, sizeof (server_addr));



server_addr.sin_family = AF_INET;

server_addr.sin_addr.s_addr = htons(INADDR_ANY);

server_addr.sin_port = htons(SERVER_PORT);

// 绑定端口

if (bind(server_socket, (struct sockaddr*) &server_addr, sizeof (server_addr)))

{

printf("Bind port %d failed.\n", SERVER_PORT);

return EXIT_FAILURE;

}



// 开始监听

if (listen(server_socket, LISTEN_QUEUE_LENGTH))

{

printf("Server Listen Failed!");

return EXIT_FAILURE;

}



// 开始处理客户端连接

bzero(&threads, sizeof(pthread_t) * MAX_THREAD_NUM);

while (1)

{

struct sockaddr_in client_addr;

socklen_t length = sizeof(client_addr);



// 建立客户端连接

int client_conn = accept(server_socket, (struct sockaddr*) &client_addr, &length);

if (client_conn < 0)

{

printf("Server Accept Failed!\n");

return EXIT_FAILURE;

}



// 新建线程, 使用新线程与客户端交互

int pthread_err = pthread_create(threads + (thread_count++), NULL, (void *)recv_data, (void *)client_conn);

if (pthread_err != 0)

{

printf("Create thread Failed!\n");

return EXIT_FAILURE;

}



}

close(server_socket);

return (EXIT_SUCCESS);

}



void recv_data(int conn_socket)

{

char buffer[BUFFER_SIZE];

bzero(buffer, BUFFER_SIZE);



int length = 0;

while (length = recv(conn_socket, buffer, BUFFER_SIZE, MSG_WAITALL))

{

if (length < 0)

{

printf("Server Recieve Data Failed! code %d\n", length);

}

printf("%s", buffer);

bzero(buffer, BUFFER_SIZE);

}

close(conn_socket);

}





int Client()

{

int cfd;

int recbytes;

int sin_size;

char buffer[1024]={0};

struct sockaddr_in s_add,c_add;

unsigned short portnum=SERVER_PORT;



printf("Hello,welcome to client !\r\n");



cfd = socket(AF_INET, SOCK_STREAM, 0);

if(-1 == cfd)

{

printf("socket fail ! \r\n");

return -1;

}

printf("socket ok !\r\n");



bzero(&s_add,sizeof(struct sockaddr_in));

s_add.sin_family=AF_INET;

s_add.sin_addr.s_addr= inet_addr("192.168.217.3");

s_add.sin_port=htons(portnum);

printf("s_addr = %#x ,port : %#x\r\n",s_add.sin_addr.s_addr,s_add.sin_port);





if(-1 == connect(cfd,(struct sockaddr *)(&s_add), sizeof(struct sockaddr)))

{

printf("connect fail !\r\n");

return -1;

}

printf("connect ok !\r\n");



if(-1 == (recbytes = read(cfd,buffer,1024)))

{

printf("read data fail !\r\n");

return -1;

}

printf("read ok\r\nREC:\r\n");



buffer[recbytes]='\0';

printf("%s\r\n",buffer);



getchar();

close(cfd);

return 0;

}

使用特权

评论回复

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

397

主题

397

帖子

0

粉丝