您的位置:首页 > 运维架构 > Linux

linux下实现服务器与客户端

2016-01-19 11:36 453 查看
#include <sys/types.h>
#include <sys/socket.h>
#include <linux/in.h>
#include <linux/un.h>
#include <string.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <errno.h>
#include <unistd.h>
#include <unistd.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <pthread.h>
#include <sys/time.h>
#include <sys/select.h>
#include <fcntl.h>
#include <unistd.h>

void *modbus_server_client_thread()
{

char buf[8192];
//char message[256];
int socket_descriptor;
struct sockaddr_in pin;
int port = 8000;
// char * str ="A default test string";
/* if(argc < 2)
{
printf("we will send a default test string.\n");

}
else
{
str = argv[1];
if(argc == 3)
{
host_name = argv[2];
}
}
*/
char * host_name ="127.0.0.1";
char  *str="tcpip_client_slave";
bzero(&pin,sizeof(pin));
pin.sin_family = AF_INET;
inet_pton(AF_INET,host_name,&pin.sin_addr);
pin.sin_port = htons(port);
if((socket_descriptor =  socket(AF_INET,SOCK_STREAM,0)) == -1)
{
perror("error opening socket \n");
exit(1);
}
if(connect(socket_descriptor,(struct sockaddr * )&pin,sizeof(pin)) == -1)
{
perror("error connecting to socket \n");
exit(1);
}

printf("sending message %s to server ..\n",str);
if( write(socket_descriptor,str,strlen(str)+1) == -1 )
{
perror("error in send \n");
exit(1);
}

printf("..sent message ...wait for message..\n");
if( read(socket_descriptor,buf,8192) == -1 )
{
perror("error in receiving response from server \n");
exit(1);
}

printf("\nResponse from server:\n\n%s\n",buf);
close(socket_descriptor);
return ;

}

void *modbus_server_slave_thread()
{
char host_name[20];
int port = 8000;
struct sockaddr_in sin,pin;
int sock_descriptor,temp_sock_descriptor,address_size;
int i , len , on=1;
char buf[16384];

sock_descriptor = socket(AF_INET,SOCK_STREAM,0);
bzero(&sin,sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port = htons(port);
if(bind(sock_descriptor,(struct sockaddr *)&sin,sizeof(sin)) == -1)
{
perror("call to bind");
exit(1);
}
if(listen(sock_descriptor,100) == -1)
{
perror("call to listem");
exit(1);
}
printf("Accpting connections...\n");

while(1)
{
address_size = sizeof(pin);
temp_sock_descriptor = accept(sock_descriptor,(struct sockaddr *)&pin,&address_size);
if(temp_sock_descriptor == -1)
{
perror("call to accept");
exit(1);
}
if(recv(temp_sock_descriptor,buf,16384,0) == -1)
{
perror("call to recv");
exit(1);
}
inet_ntop(AF_INET,&pin.sin_addr,host_name,sizeof(host_name));
printf("received from client(%s):%s\n",host_name,buf);

len = strlen(buf);
for(i = 0 ; i  < len ; i++)
{
buf[i] =  toupper(buf[i]);
}

if(send(temp_sock_descriptor,buf,len+1,0) == -1)
{
perror("call to send");
exit(1);

}
close(temp_sock_descriptor);

}

}

void main()
{
int ret;
pthread_t  modbus_client_thread,modbus_slave_thread;
pthread_create(&modbus_slave_thread,NULL,modbus_server_slave_thread,NULL);
pthread_create(&modbus_client_thread,NULL,modbus_server_client_thread,NULL);

/*等待线程结束*/
pthread_join(modbus_client_thread,NULL);//pthread_join会阻塞主线程,等待子线程结束。
pthread_join(modbus_slave_thread,NULL);

return;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  linux