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 socket 初步
- Linux Kernel 4.0 RC5 发布!
- linux lsof详解
- linux 文件权限
- Linux 执行数学运算
- 10 篇对初学者和专家都有用的 Linux 命令教程
- Linux 与 Windows 对UNICODE 的处理方式
- Ubuntu12.04下QQ完美走起啊!走起啊!有木有啊!
- 解決Linux下Android开发真机调试设备不被识别问题
- 运维入门
- 运维提升
- Linux 自检和 SystemTap
- Ubuntu Linux使用体验
- c语言实现hashmap(转载)
- Linux 信号signal处理机制
- linux下mysql添加用户
- Scientific Linux 5.5 图形安装教程
- Linux 下无损图片压缩小工具介绍