导读:
服务器端:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define SERVPORT 16200 // 服务器监听端口
#define BACKLOG 5 // 同一时间监听的最大数目
#define BUF 200
// 客户端链接的线程
void* tcp_thread (void* fd)
{
int newfd =*((int*)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) == true )
{
FD_CLR(newfd, &rfds);
char data[BUF];
int res = read(newfd, data, 1024); // sizeof(data)
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 is:/n%s/n", data);
}
}
else
{
printf("wait.../n");
sleep(2000);
continue;
}
}
}
int main()
{
void* tcp_thread (void* );
int sockfd, *pfd;
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;
}
while ( 1 )
{
int sin_size = sizeof(struct sockaddr);
pfd = (int*)malloc(sizeof(int));
bzero(pfd,sizeof(int));
if( (*pfd = accept(sockfd, (struct sockaddr *)&remote_addr, (socklen_t*)&sin_size)) <0)
{
perror("accept error/n");
continue;
}
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,pfd); //(void *)client_fd
pthread_attr_destroy(&attr);
}
return 1;
}
客户端:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define BUF 200
int main ()
{
int fid = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in their_addr;
struct hostent *host;
if((host = gethostbyname("127.0.0.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(16200);
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");
}
char cDatabuff[BUF] = {0};
while ( 1 )
{
scanf("%s", cDatabuff);
// 当在客户端输入123时就结束向客户端发送数据
if ( strcmp(cDatabuff, "123") == 0 )
{
break;
}
write(fid, cDatabuff, strlen(cDatabuff)+1);
}
return 1;
}
本文转自
http://cailong1987.blog.163.com/blog/static/8924701200752132235596/
服务器端:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define SERVPORT 16200 // 服务器监听端口
#define BACKLOG 5 // 同一时间监听的最大数目
#define BUF 200
// 客户端链接的线程
void* tcp_thread (void* fd)
{
int newfd =*((int*)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) == true )
{
FD_CLR(newfd, &rfds);
char data[BUF];
int res = read(newfd, data, 1024); // sizeof(data)
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 is:/n%s/n", data);
}
}
else
{
printf("wait.../n");
sleep(2000);
continue;
}
}
}
int main()
{
void* tcp_thread (void* );
int sockfd, *pfd;
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;
}
while ( 1 )
{
int sin_size = sizeof(struct sockaddr);
pfd = (int*)malloc(sizeof(int));
bzero(pfd,sizeof(int));
if( (*pfd = accept(sockfd, (struct sockaddr *)&remote_addr, (socklen_t*)&sin_size)) <0)
{
perror("accept error/n");
continue;
}
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,pfd); //(void *)client_fd
pthread_attr_destroy(&attr);
}
return 1;
}
客户端:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define BUF 200
int main ()
{
int fid = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in their_addr;
struct hostent *host;
if((host = gethostbyname("127.0.0.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(16200);
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");
}
char cDatabuff[BUF] = {0};
while ( 1 )
{
scanf("%s", cDatabuff);
// 当在客户端输入123时就结束向客户端发送数据
if ( strcmp(cDatabuff, "123") == 0 )
{
break;
}
write(fid, cDatabuff, strlen(cDatabuff)+1);
}
return 1;
}
本文转自
http://cailong1987.blog.163.com/blog/static/8924701200752132235596/