linux下Socket通信之运行实例
来源:互联网 发布:tushy最美女演员知乎 编辑:程序博客网 时间:2024/04/27 11:18
导读:
服务器端:
#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
#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/
- linux下Socket通信之运行实例
- linux下socket通信之通信模型
- linux下socket通信之通信模型
- linux下socket通信之通信模型
- Linux下Socket通信(IPC),附实例
- 嵌入式linux下socket网络通信编程实例一
- linux之socket通信
- linux下c/c++实例之十socket服务器实例
- Linux下IPC机制之Socket通信总结
- Linux系统下socket通信注意之bind函数
- linux-windows下socket通信
- linux下socket通信概述
- Linux下socket通信总结
- Linux环境下socket通信
- linux下的socket通信
- Linux下Socket简单通信
- linux下c/c++实例之十socket简单应用
- linux下c/c++实例之十socket简单应用
- ASP.Net的Cookie实现
- XML+RDF——实现Web数据基于语义的描述
- 基于语义Web技术的MIS与基于XML技术,传统MIS的比较
- RDF与XML的区别是什么
- 数据库设计范式
- linux下Socket通信之运行实例
- Debug 和 Release 区别
- 周杰伦 jay《最长的电影》mp3 下载/试听/MV/在线播放
- Linux系统启动的标准流程
- 感冒
- 仙界修仙 - 最新章节列表
- [ASP.NET] Session 详解
- Linux网络编程:2. 初等网络函数介绍(TCP)
- 鸳鸯梦-小说