Linux网络编程 之 TCP 多线程的服务器和客户端同时收发数据
linux-dash
A beautiful web dashboard for Linux
项目地址:https://gitcode.com/gh_mirrors/li/linux-dash
免费下载资源
·
具体的相关TCP 的资料我这里就不多说,我会在末尾贴出来。
直接上源码吧,里面有一些跟本题无关的我已经屏弊掉了,里面的注释也很清楚,大家只要自己看一下就应该知道了。
1、服务器端代码(Server.c)
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/fcntl.h>
#include <pthread.h>
#include <unistd.h>
#define SERVER_PORT 5500
#define BUF_SIZE 1024
#define LISNUM 10
int main(){
int sock_fd, new_fd;
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;
int sin_size,myport,lisnum;
int n;
char s_net2usb_buff[BUF_SIZE+1];
//char s_usb2net_buff[BUF_SIZE+1];
char s_usb2net_buff[BUF_SIZE+1] = "The information is from server.The server will send message frequtlly and you will always know it.If you is connected,you could see the information!Have a good time!";
bool usb_connect,net_connect ;
int on = 1;
pthread_t s_usb2net_id,s_net2usb_id;
/* open the point*/
/* int fp = open("/dev/impinj",O_RDWR);
if(!fp){
perror("open device ERROR");
exit(0);
}else{
printf("open device success!\n");
}
*/
/* if(argc!=2){
printf(stderr,"Usage:%s n\a\n",argv[0]);
exit(1);
}
if((n = atoi(argv[1])) < 0){
printf(stderr,"Usage:%s n\a\n",argv[0]);
exit(1);
}
if(argv[1])
myport = atoi(argv[1]);
else
myport = 7838;
if(argv[2])
lisnum = atoi(argv[2]);
else
lisnum = 10;
*/
if ((sock_fd = socket(AF_INET,SOCK_STREAM,0)) == -1){
perror("socket");
exit(1);
}
/* Enable address reuse */
setsockopt(sock_fd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on));
bzero(&server_addr,sizeof(struct sockaddr_in));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(SERVER_PORT);
if(bind(sock_fd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr)) == -1){
perror("bind");
exit(1);
}
printf("Hello,welcome to server!\n");
if(listen(sock_fd,LISNUM) == -1){
perror("listen");
exit(1);
}
while(1){
sin_size = sizeof(struct sockaddr_in);
if ((new_fd = accept(sock_fd,(struct sockaddr *)&(client_addr),&sin_size))==-1) {
perror("accept");
// continue;
exit(1);
}else{
if(send(new_fd,"connect to server success!",30,0) == -1){
perror("send");
close(new_fd);
exit(0);
}
}
printf("server:got connection from %s,port %d,socket %d\n",inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port),new_fd);
/*the read and send thread */
void *S_UsbtoNet_Thread(void *arg){
/* char writebuff[] = {0x01,0x00,0x00,0x07,0xFF,0xFF,0x00,0x00};
printf("You want to write:%s\n",writebuff);
if(8 == write(fp,writebuff,8)){
printf("write success!\n");
}else{
printf("write failed!please write the command:");
char str[80];
scanf("%s",&str);
write(fp,str,strlen(str));
}
char writebuff_1[] = {0x01,0x00,0x00,0xF0,0x0F,0x00,0x00,0x00};
printf("You want to write_1:%s\n",writebuff_1);
if(8 == write(fp,writebuff_1,8)){
printf("write success!\n");
}else{
printf("write failed!please write the command:");
char str[80];
scanf("%s",&str);
write(fp,str,strlen(str));
}
*/
while(1){
/* if((num = read(fp,s_usb2net_buff,BUF_SIZE)) < 0){
perror("Read ERROR");
}else{
printf("Read buff %d bytes:%s\n",num,s_usb2net_buff);
memset(s_usb2net_buff,0,1024);
// close(fp);
}*/
// if(num > 0){
if(send(new_fd,s_usb2net_buff,strlen(s_usb2net_buff),0) == -1){
perror("send");
//close(new_fd);
//exit(0);
}else{
printf("server send success!\n");
}
// }
}
}
/*the receive and write thread */
void *S_NettoUsb_Thread(void *arg){
while(1){
bzero(s_net2usb_buff,BUF_SIZE+1);
if ( (n = recv(new_fd,s_net2usb_buff,BUF_SIZE,0 ))== -1) {
perror("server recv ERROR");
//exit(1);
}
s_net2usb_buff[n] = 0;
if(n > 0){
printf("Server recv %d bytes:%s\n",n,s_net2usb_buff);
/* if(strlen(s_net2usb_buff) == write(fp,s_net2usb_buff,strlen(s_net2usb_buff))){
printf("write success!\n");
}else{
printf("write failed!\n");
}*/
}
}
}
/* create the receive and write thread */
if(pthread_create(&s_net2usb_id,NULL,S_NettoUsb_Thread,NULL)){
printf("S_NettoUsb_thread is created failed!\n");
//return -1;
}
/* create the read and send thread */
if(pthread_create(&s_usb2net_id,NULL,S_UsbtoNet_Thread,NULL)){
printf("S_UsbtoNet_thread is created failed!\n");
//return -1;
}
if((pthread_join(s_net2usb_id,NULL)) != 0){
perror("pthread_join ERROR");
}
if((pthread_join(s_usb2net_id,NULL)) != 0){
perror("pthread_join ERROR");
}
}
close(sock_fd);
exit(0);
}
服务器端编译:gcc -lpthread Server.c -o server
运行:./server
2、客户端代码(Clinet.c)
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <netdb.h>
#include <pthread.h>
#define MAXDATASIZE 1024
int main(int argc, char *argv[]){
int sockfd, portnumber,nbytes;
char c_usb2net_buf[MAXDATASIZE] = "Hello,show you are success to send word from clinet if you can see this sentence! The programe took my three day to make it work!And their is a will,there is a way!Enjoy your time!Best Regards!";
char c_net2usb_buf[MAXDATASIZE];
bool conn = true,send_thread = true;
struct hostent *he;
struct sockaddr_in server_addr;
int i;
pthread_t c_usb2net_id,c_net2usb_id;
if(argc != 3){
fprintf(stderr,"Usage:%s hostname portnumber\a\n",argv[0]);
exit(1);
}
if ( (portnumber = atoi(argv[2])) < 0 ) {
fprintf(stderr,"Usage:%s hostname portnumber\a\n",argv[0]);
exit(1);
}
if ( (he = gethostbyname(argv[1])) == NULL ) {
perror("gethostbyname");
exit(1);
}
if ( (sockfd = socket(AF_INET,SOCK_STREAM,0))== -1) {
perror("socket");
exit(1);
}
bzero(&server_addr,sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(portnumber);
server_addr.sin_addr = *((struct in_addr *)he->h_addr);
while(1){
while(conn){
if(connect(sockfd, (struct sockaddr *)&server_addr,sizeof(struct sockaddr)) == -1){
perror("connect");
exit(1);
}
conn = false;
}
/***************** the send thread function ********************/
void *C_UsbtoNet_Thread(void *arg){
i=0;i++;
printf("send thread ------>%d",i);
while(1){
if(send(sockfd,c_usb2net_buf,strlen(c_usb2net_buf),0) == -1){
perror("send");
// close(sockfd);
}else if(send(sockfd,c_usb2net_buf,strlen(c_usb2net_buf),0) == 0){
printf("----> clinet is ranking and waiting!\n");
}else{
printf("----> clinet send success!\n");
}
}
}
/* create the thread */
while(send_thread){
if(pthread_create(&c_usb2net_id,NULL,C_UsbtoNet_Thread,NULL)){
printf("C_UsbtoNet_thread is created failed!\n");
send_thread = true;
}
send_thread = false;
}
/* stock the thread */
/*if((pthread_join(c_usb2net_id,NULL)) != 0){
perror("usb2net_thread_join ERROR");
}*/
/**************** the receive action in the main thread ***********/
bzero(c_net2usb_buf,MAXDATASIZE+1);
if ( (nbytes = recv(sockfd,c_net2usb_buf,MAXDATASIZE,0 ))== -1) {
perror("server recv ERROR");
//exit(1);
}
c_net2usb_buf[nbytes] = 0;
if(nbytes > 0){
printf("clinet recv %d bytes:%s\n",nbytes,c_net2usb_buf);
}
}
close(sockfd);
exit(0);
}
客户端编译:gcc -lpthread Clinet.c -o clinet
运行:./clinet 192.168.1.121 5500
运行结果截图如下:
源代码和相关资料下载:http://download.csdn.net/detail/yanyuanfen2011/6595187
如有问题,欢迎大家留言交流讨论!
GitHub 加速计划 / li / linux-dash
6
1
下载
A beautiful web dashboard for Linux
最近提交(Master分支:4 个月前 )
186a802e
added ecosystem file for PM2 4 年前
5def40a3
Add host customization support for the NodeJS version 4 年前
更多推荐
已为社区贡献3条内容
所有评论(0)