当前位置:
文档之家› Linux环境下Server与Client交互(Socket+多线程)
Linux环境下Server与Client交互(Socket+多线程)
if(ret < 0)
{
cout<<ERROR<<"Fail to create sending data thread!"<<endl;
return false;
}
param2.sockfd = sockfd;
param2.fp = stdout;
ret = pthread_create(&thread2, NULL, ReceiveData_Client, ¶m2);
{
if(pid != 0)
newSockFd = accept(sockfd, (struct sockaddr *)&theirAddr, &sin_size);
if(newSockFd < 0)
{
cout<<ERROR<<"error in accept"<<endl;
cout<<"pid is "<<pid<<endl;
#define ARRAY_SIZE 256
typedef struct sockaddr_in SockAddr;
typedef struct Parameters_t
{
FILE* fp;
int sockfd;
}Parameters;
void* SendData_Client(void* param)
if(sockfd < 0)
{
cout<<ERROR<<"Fail to creat socket in server!"<<endl;
return -1;
}
mySerAddr.sin_family = AF_INET;
mySerAddr.sin_port = htons(MYPORT);
mySerAddr.sin_addr.s_addr = htonl(INADDR_ANY);
{
Parameters param1, param2;
pthread_t thread1 = sockfd;
param1.fp = fp;
ret = pthread_create(&thread1, NULL, SendData_Client, ¶m1);
}
}
}
void* ReceiveData_Client(void* param)
{
Parameters* pParam = (Parameters*)param;
char recvs[ARRAY_SIZE];
int n = 0;
while(1)
{
if((n = recv((*pParam).sockfd, recvs, ARRAY_SIZE, 0)) == 0)
{
close(sockfd);
CreateThread(stdin, newSockFd);
close(newSockFd);
}
else
{
{
//wait for child-process to exit.
pid_t pid2;
pid2 = wait(NULL);
cout<<"Parent process catch child process id:!"<<pid2<<endl;
return -1;
}
else
{
cout<<"pid is "<<pid<<endl;
cout<<INFO<<"Create new socket for client:"<<(*(SockAddr*)&theirAddr).sin_addr.s_addr<<endl;
}
if((pid = fork()) == 0)
return -1;
}
if((phost = gethostbyname(argv[1])) == NULL)
{
cout<<ERROR<<"Fail to get hostname!"<<endl;
return -1;
}
struct in_addr** addrs;
}
close(newSockFd);
}
}
close(sockfd);
}
客户端代码
#include<stdlib.h>
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netdb.h>
#include<iostream>
{
recvs[n-1] = 0;
cout<<RECEIVE<<recvs<<endl;
cout<<"n is "<<n<<endl;
}
}
return NULL;
}
void* SendData_Server(void* param)
{
char sends[ARRAY_SIZE] = {0};
int n = 0;
if(ret < 0)
{
cout<<ERROR<<"Fail to create Receive data thread!"<<endl;
return false;
}
//wait for child-threads to exit;
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
param1.sockfd = sockfd;
pFun = ReceiveData_Server;
ret = pthread_create(&pThread1, NULL, ReceiveData_Server, ¶m1);
if(ret != 0)
{
cout<<ERROR<<"Fail to create receive thread!"<<endl;
#define MYPORT 3490
#define BACKLOG 10
#define ARRAY_SIZE 256
using namespace std;
typedef struct sockaddr_in SockAddr;
typedef struct Parameters_t
{
FILE* fp;
#include<pthread.h>
using namespace std;
#define ERROR "ERROR:"
#define SEND "SEND:"
#define RECEIVE "RECEIVE:"
#define INFO "INFO"
#define CLIENT_PORT 3490
return -1;
}
ret = listen(sockfd, BACKLOG);
if(ret < 0)
{
cout<<ERROR<<"Fail to listen!"<<endl;
return -1;
}
else
{
cout<<INFO<<"Server is listenning!"<<endl;
}
while(1)
{
return NULL;
}
else if(n < 0)
{
cout<<INFO<<"Fail to receive message"<<endl;
}
else if(n > 0)
{
recvs[n-1] = 0;
cout<<RECEIVE<<recvs<<endl;
}
}
return NULL;
}
bool CreateThread(FILE* fp, int sockfd)
bzero(&(mySerAddr.sin_zero), 8);
ret = bind(sockfd, (const struct sockaddr *)&mySerAddr, sockLen);
if(ret < 0)
{
cout<<ERROR<<"Fail to bind socket!"<<endl;
return false;
}
param2.fp = stdin;
param2.sockfd = sockfd;
ret = pthread_create(&pThread2, NULL, SendData_Server, ¶m2);
if(ret != 0)
{
cout<<ERROR<<"Fail to create send thread!"<<endl;