|
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<pthread.h>
#define PORT 1234
#define BACKLOG 5
#define MAXDATASIZE 1000
void process_cli(int connectfd,struct sockaddr_in client);
void savedata(char *recvbuf,int len,char *cli_data);
void *start_routine(void *arg);
struct ARG
{
int connfd;
struct sockaddr_in client;
};
main()
{
int listenfd,connectfd;
struct sockaddr_in server;
struct sockaddr_in client;
int sin_size;
pthread_t thread;
struct ARG *arg;
if((listenfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("Creating socket failed!\n");
exit(1);
}
int opt=SO_REUSEADDR;
setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));
bzero(&server,sizeof(server));
server.sin_family=AF_INET;
server.sin_port=htons(PORT);
server.sin_addr.s_addr=htonl(INADDR_ANY);
if(bind(listenfd,(struct sockaddr *)&server,sizeof(struct sockaddr))==-1)
{
perror("bind error\n");
exit(1);
}
if(listen(listenfd,BACKLOG)==-1)
{
perror("listen error\n");
exit(1);
}
sin_size=sizeof(struct sockaddr_in);
while(1)
{
if((connectfd=accept(listenfd,(struct sockaddr *)&client,&sin_size))==-1)
{
perror("accept error\n");
exit(1);
}
arg=(struct ARG *)malloc(sizeof(struct ARG));
arg->connfd=connectfd;
memcpy((void *)&arg->client,&client,sizeof(client));
if(pthread_create(&thread,NULL,start_routine,(void *)arg))
{
perror("pthread_create() error\n");
exit(1);
}
}
close(listenfd);
}
void process_cli(int connectfd,struct sockaddr_in client)
{
char cli_data[5000];
int num,i;
char recvbuf[MAXDATASIZE],sendbuf[MAXDATASIZE],cli_name[MAXDATASIZE];
printf("you got a connection from %s. ",inet_ntoa(client.sin_addr));
num=recv(connectfd,cli_name,MAXDATASIZE,0);
if(num==0)
{
close(connectfd);
printf("client disconnected.\n");
return;
}
cli_name[num-1]='\0';
printf("client's name is %s\n",cli_name);
while(num=recv(connectfd,recvbuf,MAXDATASIZE,0))
{
recvbuf[num]='\0';
printf("Received client(%s) message:%s",cli_name,recvbuf);
savedata(recvbuf,num,cli_data);
for(i=0;i<num-1;i++)
{
sendbuf=recvbuf[num-i-2];
}
sendbuf[num-1]='\0';
send(connectfd,sendbuf,strlen(sendbuf),0);
}
close(connectfd);
printf("Client(%s) closed connection. user'data:%s\n",cli_name,cli_data);
}
void *start_routine(void *arg)
{
struct ARG *info;
info=(struct ARG *)arg;
process_cli(info->connfd,info->client);
free(arg);
pthread_exit(NULL);
}
void savedata(char *recvbuf,int len,char *cli_data)
{
int i;
static int index =0;
for(i=0;i<len-1;i++)
{
cli_data[index++]=recvbuf;
}
cli_data[index]='\0';
}
运行结果:
服务器:
#./multiserver
you got a connection from 127.0.0.1.
you got a connection from 127.0.0.1.
client's name is client1
client's name is client2
Received client(client1) message:1234
Received client(client2) message:abc
Received client(client1) message:a
Received client(client2) message:d
Client(client1) closed connection. user'data:1234
Client(client2) closed connection. user'data:
客户端1
$./itrclient 127.0.0.1
connected to server.
input name : client1
Input string to server:1234
server message: 4321
Input string to server:a
server message: a
Input string to server:^D
exit
$
客户2:
./itrclient 127.0.0.1
connected to server.
input name : client2
Input string to server:abc
server message: cba
Input string to server:d
server message: d
Input string to server:^D
exit
用户1关闭时显示用户数据“1234”
2关闭时显示“ ”。
我觉得应该输出:"1234a" 和”abcd “阿
原因时什么阿。分析一下好么。 |
|