Linux下基于TCP的线程通信
/*
*server.c
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <pthread.h>
#define BACKLOG 10
#define MYPORT 8888
#define SIZE 1024
int sfd, new_fd;
void receive_message()
{
int nbytes;
char buf[SIZE];
while(1)
{
bzero(buf, SIZE);
nbytes = recv(new_fd, buf, SIZE, 0);
if(nbytes<=0)
{
perror("recv");
exit(1);
}
buf[nbytes] = '\0';
if(strcmp(buf, "quit") == 0)
{
printf("Client is quit\n");
close(new_fd);
close(sfd);
exit(1);
}
printf("Receive message from cLient: %s\n", buf);
}
}
void accept_connect()
{
int sin_size;
int ret = 0;
pthread_t tid;
struct sockaddr_in client_addr;
sin_size = sizeof(struct sockaddr_in);
new_fd = accept(sfd, (struct sockaddr*)&client_addr, &sin_size);
if(-1 == new_fd)
{
perror("accept");
exit(1);
}
printf("Server get connected from %s\n", inet_ntoa(client_addr.sin_addr));
ret = pthread_create(&tid, NULL, (void *)receive_message, NULL);
if(ret != 0)
{
printf("pthread create failed\n");
exit(1);
}
}
int main()
{
int ret=0;
pthread_t tid;
struct sockaddr_in myaddr;
sfd = socket(AF_INET, SOCK_STREAM, 0);
if(-1== sfd)
{
perror("socket");
close(sfd);
exit(1);
}
bzero(&myaddr, sizeof(myaddr));
myaddr.sin_family = AF_INET;
myaddr.sin_port = htons(MYPORT);
myaddr.sin_addr.s_addr = INADDR_ANY;
if(bind(sfd, (struct sockaddr*)&myaddr, sizeof(struct sockaddr)) == -1)
{
perror("bind()");
close(sfd);
exit(1);
}
if(listen(sfd, BACKLOG) == -1)
{
perror("listen()");
close(sfd);
exit(1);
}
ret = pthread_create(&tid, NULL, (void *)accept_connect, NULL);
if(0 != ret)
{
printf("pthread create failed\n");
exit(1);
}
char msg[SIZE];
while(1)
{
bzero(msg, SIZE);
scanf("%s", msg);
if(send(new_fd, msg, strlen(msg), 0) == -1)
{
perror("send()");
close(new_fd);
exit(1);
}
if(strcmp(msg, "quit") == 0)
{
printf("Byebye!\n");
close(new_fd);
close(sfd);
exit(1);
}
}
return 0;
}
/*
*client.c
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <pthread.h>
#define BACKLOG 10
#define MYPORT 8888
#define SIZE 1024
int sfd;
void receive_message()
{
int nbytes;
char buf[SIZE];
while(1)
{
bzero(buf, SIZE);
nbytes = recv(sfd, buf, SIZE, 0);
if(nbytes<=0)
{
perror("recv()");
exit(1);
}
buf[nbytes] = '\0';
if(strcmp(buf, "quit") == 0)
{
printf("Server is quit\n");
close(sfd);
exit(1);
}
printf("Receive message from server: %s\n", buf);
}
}
int main(int argc, char *argv[])
{
int ret=0;
char msg[SIZE];
pthread_t tid;
struct sockaddr_in server_addr;
struct hostent *hent;
if(argc != 2)
{
fprintf(stderr, "usage: <client hostname>\n");
exit(1);
}
hent = gethostbyname(argv[1]);
if(hent == NULL)
{
perror("gethostbyname()");
exit(1);
}
sfd = socket(AF_INET, SOCK_STREAM, 0);
if(-1== sfd)
{
herror("socket");
close(sfd);
exit(1);
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(MYPORT);
server_addr.sin_addr.s_addr = ((struct in_addr *)hent->h_addr)->s_addr;
//server_addr.sin_addr.s_addr = inet_addr(argv[1]);
bzero(&server_addr.sin_zero, 8);
if(connect(sfd, (struct sockaddr*)&server_addr, sizeof(struct sockaddr_in))==-1)
{
perror("connect()");
close(sfd);
exit(1);
}
ret = pthread_create(&tid, NULL, (void *)receive_message, NULL);
if(0 != ret)
{
printf("pthread create failed\n");
exit(1);
}
while(1)
{
bzero(msg, SIZE);
scanf("%s", msg);
if(send(sfd, msg, strlen(msg), 0) == -1)
{
perror("send()");
close(sfd);
exit(1);
}
if(strcmp(msg, "quit") == 0)
{
printf("Byebye!\n");
close(sfd);
exit(1);
}
}
return 0;
}