linux socketプログラミング(マルチスレッド)
6988 ワード
1、クライアント
2,サーバ側
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
/*
: , ( ) ,
, , 。
*/
typedef struct _recvmodel
{
int st;
struct sockaddr_in * addr;
} RecvModel;
//send message
void * send_thread(void *arg)
{
if (arg == NULL)
{
printf("param is not allow NULL!
");
return NULL;
}
int st = *(int *) arg;
char buf[1024] = { 0 };
while (1)
{
read(STDIN_FILENO, buf, sizeof(buf));
if (send(st, buf, strlen(buf), 0) == -1)
{
printf("send failed ! error message %s
", strerror(errno));
return NULL;
}
memset(buf, 0, sizeof(buf));
}
return NULL;
}
//recv message
void * recv_thread(void * arg)
{
if (arg == NULL)
{
printf("param is not allow NULL!
");
return NULL;
}
RecvModel * model = (RecvModel *) arg;
int flag = 0;
char buf[1024] = { 0 };
while (1)
{
flag = recv(model->st, buf, sizeof(buf), 0);
if (flag == 0)
{
printf(" !
");
return NULL;
} else if (flag == -1)
{
printf("recv failed ! error message : %s
", strerror(errno));
return NULL;
}
printf("from %s:%s", inet_ntoa(model->addr->sin_addr), buf);
memset(buf, 0, sizeof(buf));
}
return NULL;
}
int main(int arg, char *args[])
{
// socket
int st = socket(AF_INET, SOCK_STREAM, 0);
if (st == -1)
{
printf("open socket failed! error message:%s
", strerror(errno));
return -1;
}
// IP
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
// TCP/IP
addr.sin_family=AF_INET;
//
addr.sin_port = htons(8080);
//
addr.sin_addr.s_addr = inet_addr("192.168.1.102");
//connect server
int numx = connect(st, (struct sockaddr *) &addr, sizeof(addr));
if (numx == -1)
{
printf("connect server failed ! error message :%s
", strerror(errno));
goto END;
}
RecvModel model;
model.st = st;
model.addr = &addr;
// -- 1 , 2
pthread_t thr1, thr2;
if (pthread_create(&thr1, NULL, send_thread, &st) != 0)
{
printf("create thread failed !
");
goto END;
}
if (pthread_create(&thr2, NULL, recv_thread, &model) != 0)
{
printf("create thread failed !
");
goto END;
}
pthread_join(thr1, NULL);
pthread_join(thr2, NULL);
END: close(st);
return 0;
}
2,サーバ側
// --
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
typedef struct _recvmodel
{
int st;
struct sockaddr_in * addr;
} RecvModel;
//send message
void * send_thread(void *arg)
{
if (arg == NULL)
{
printf("param is not allow NULL!
");
return NULL;
}
int st = *(int *) arg;
char buf[1024] = { 0 };
while (1)
{
read(STDIN_FILENO, buf, sizeof(buf));
if (send(st, buf, strlen(buf), 0) == -1)
{
printf("send failed ! error message %s
", strerror(errno));
return NULL;
}
memset(buf, 0, sizeof(buf));
}
return NULL;
}
//recv message
void * recv_thread(void * arg)
{
if (arg == NULL)
{
printf("param is not allow NULL!
");
return NULL;
}
RecvModel * model = (RecvModel *) arg;
int flag = 0;
char buf[1024] = { 0 };
while (1)
{
flag = recv(model->st, buf, sizeof(buf), 0);
if (flag == 0)
{
printf(" !
");
return NULL;
} else if (flag == -1)
{
printf("recv failed ! error message : %s
", strerror(errno));
return NULL;
}
printf("from %s:%s", inet_ntoa(model->addr->sin_addr), buf);
memset(buf, 0, sizeof(buf));
}
return NULL;
}
int main(int arg, char *args[])
{
//short port = atoi(args[1]);
// socket
int st = socket(AF_INET, SOCK_STREAM, 0);
if (st == -1)
{
printf("open socket failed! error message:%s
", strerror(errno));
return -1;
}
//
int on = 1;
if (setsockopt(st, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1)
{
printf("setsockpot failed ! error message %s
", strerror(errno));
goto END;
}
// IP
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
// TCP/IP
addr.sin_family = AF_INET;
//
addr.sin_port = htons(8080);
//
addr.sin_addr.s_addr = htonl(INADDR_ANY);
//bind ip
if (bind(st, (struct sockaddr *) &addr, sizeof(addr)) == -1)
{
printf("bind ip failed ! error message :%s
", strerror(errno));
goto END;
}
//
if (listen(st, 20) == -1)
{
printf("listen failed ! error message :%s
", strerror(errno));
goto END;
}
// ( )
struct sockaddr_in client_addr;
memset(&client_addr, 0, sizeof(client_addr));
socklen_t client_addrlen = sizeof(client_addr);
// : client_addrlen client_addrlen , client_addrlen client_addr ,
// client_addrlen 0 , struct sockaddr_in ,
int client_st = accept(st, (struct sockaddr *) &client_addr,
&client_addrlen);
if (client_st == -1)
{
printf("accept failed ! error message :%s
", strerror(errno));
goto END;
}
RecvModel model;
model.st = client_st;
model.addr = &client_addr;
printf("accept by=%s
",inet_ntoa(client_addr.sin_addr));
// -- 1 , 2
pthread_t thr1, thr2;
if (pthread_create(&thr1, NULL, send_thread, &client_st) != 0)
{
printf("create thread failed !
");
goto END;
}
if (pthread_create(&thr2, NULL, recv_thread, &model) != 0)
{
printf("create thread failed !
");
goto END;
}
pthread_join(thr1, NULL);
pthread_join(thr2, NULL);
//
close(client_st);
END: close(st);
return 0;
}