/*server.c*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <unistd.h>
#include <strings.h>
#include <pthread.h>
#define SERVPORT 1620 /* server listen port */
#define BACKLOG 20 /* max number of linking at a time */
#define BUF 100
int main()
{
void* tcp_thread (void* );
int sockfd,client_fd; /*sock_fd:listen socket;client_fd:data transport socket */
int on=1;
struct sockaddr_in my_addr; /* information of server address */
struct sockaddr_in remote_addr;/* information of client address */
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)/* socket create */
{
perror( "socker create error\n ");
return 0;
}
bzero(&my_addr,sizeof(my_addr));
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(SERVPORT);
my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,(void *)&on,sizeof(int));
if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) //bind
{
perror( "bind error\n ");
return 0;
}
if (listen(sockfd, BACKLOG) < 0) //listen
{
perror( "listen error\n ");
return 0;
}
printf( "sockfd:%d\n ", sockfd);
while(1)
{
int sin_size = sizeof(struct sockaddr);
if((client_fd=accept(sockfd,(struct sockaddr *)&remote_addr,(socklen_t*)&sin_size)) < 0)
{
perror( "accept error\n ");
continue;
}
printf( "client_fd:%d\n ",client_fd);
char ptr[32];
inet_ntop(AF_INET,(void *)&remote_addr.sin_addr,ptr,32);
printf( "received a connection from %s\n ", ptr);
pthread_t thread_id;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); /* the attribute of the thread */
pthread_create(&thread_id,&attr,tcp_thread,(void *)client_fd);
pthread_attr_destroy(&attr);
}
return 1;
}
void* tcp_thread (void* fd) //thread for a client link
{
int newfd = (int)fd;
printf( "newfd:%d\n ",newfd);
while (1)
{
fd_set rfds;
FD_ZERO( &rfds);
FD_SET(newfd, &rfds);
select(FD_SETSIZE, &rfds, NULL, NULL, NULL);
if (FD_ISSET(newfd, &rfds))
{
FD_CLR(newfd, &rfds);
char data[BUF];
memset(data, '\0', sizeof(data));
int res = read(newfd, data, sizeof(data));
printf( "the mes is %d\n ",res);
if (res < 0)
{
perror( "Communication error\n ");
return NULL;
}
else if(res == 0)
{
printf( "Communication end\n ");
shutdown(newfd,SHUT_RDWR);
return NULL;
}
else
{
printf( "date:%s\n ",data);
}
}
else
{
continue;
}
}
}
/*client.c*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <unistd.h>
#include <strings.h>
#include <netdb.h>
#include <pthread.h>
#define BUF 100
void* pthfunction (void* fd) //thread for a client link
{
int newfd = (int)fd;
printf( "newfd:%d\n ",newfd);//new pthread fd
while (1)
{
fd_set rfds;
FD_ZERO( &rfds);
FD_SET(newfd, &rfds);
select(FD_SETSIZE, &rfds, NULL, NULL, NULL);
if (FD_ISSET(newfd, &rfds))
{
FD_CLR(newfd, &rfds);
char data[BUF];
scanf( "%s ",data);
memset(data, '\0', sizeof(data));//clean 0
int res = write(newfd,data,strlen(data));
printf( "the mes is %d\n ",res);
if (res < 0) //fail
{
perror( "Communication error\n ");
return NULL;
}
else if(res == 0) //interrupt
{
printf( "Communication end\n ");
shutdown(newfd,SHUT_RDWR);
return NULL;
}
else //success
{
printf( "date:%s\n ",data);
}
}
else
{
continue;
}
}
}
int main (int argc,char *argv[])
{
if (argc < 2)
{
perror( "Please enter the server 's hostname!\n ");
exit(1);
}
int fid = socket(AF_INET, SOCK_STREAM, 0);
if(fid == -1)
{
perror( "socket error!\n ");
exit(1);
}
struct sockaddr_in their_addr;
struct hostent *host;
if((host = gethostbyname(argv[1])) == NULL)
{
perror( "gethostbyname error!\n ");
exit(1);
}
bzero(&their_addr,sizeof(their_addr));
their_addr.sin_family = AF_INET;
their_addr.sin_port = htons(1620);
their_addr.sin_addr = *((struct in_addr *)host-> h_addr);
if(connect(fid,(struct sockaddr *)&their_addr,sizeof(struct sockaddr)) == -1)
{
printf( "connect error\n ");
exit(1);
}
while(1)
{
int len_clt,sendfd;
len_clt = sizeof(their_addr);
if((sendfd = accept(&fid,(struct sockaddr *)&their_addr,(socklen_t*)&len_clt)) == -1)
{
perror( "accept error\n ");
exit(1);
}
pthread_t thread_id;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); // the attribute of the thread
pthread_create(&thread_id,&attr,pthfunction,(void *)sendfd);
pthread_attr_destroy(&attr);
// write(fid,data,strlen(data));
//create a pthread to send message to server
}
return 1;
}