TCP多线程通信客户端代码

2023-05-16

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <pthread.h>
#define MAXLINE 100;
void *threadsend(void *vargp);
void *threadrecv(void *vargp);
int main(int argc,char **argv) {
	int *clientfdp;
	clientfdp = (int *)malloc(sizeof(int));
	*clientfdp = socket(AF_INET,SOCK_STREAM,0);
	struct sockaddr_in serveraddr;
	struct hostent *hp;
	bzero((char *)&serveraddr,sizeof(serveraddr));
	serveraddr.sin_family = AF_INET;
	serveraddr.sin_port = htons(atoi(argv[2]));
	serveraddr.sin_addr.s_addr = inet_addr(argv[1]);
	if(connect(*clientfdp,(struct sockaddr *)&serveraddr,sizeof(serveraddr)) ==-1) {
		printf("connect error\n");
		exit(1);
	}
	pthread_t tid1,tid2;
	printf("connected\n");
	while(1) {
		pthread_create(&tid1,NULL,threadsend,clientfdp);
		pthread_create(&tid2,NULL,threadrecv,clientfdp);
	}
	return EXIT_SUCCESS;
}
void *threadsend(void * vargp) {
//pthread_t tid2;
	int connfd = *((int *)vargp);
	int idata;
	char temp[100];
                  //printf("xiaoyuqi: ");
	while(1) {
                
		fgets(temp,100,stdin);
		send(connfd,temp,100,0);
		printf("          client send OK\n");
	}
	printf("client send\n");
	return NULL;
}
void *threadrecv(void *vargp) {
	char temp[100];
	int connfd = *((int *)vargp);
	while(1) {
		int idata = 0;
		idata = recv(connfd,temp,100,0);
		if(idata > 0) {
			printf("dingwenjiao: %s",temp);
		}
	}
	return NULL;
}

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

TCP多线程通信客户端代码 的相关文章

随机推荐