// File: 64.cpp
//g++ -o 64 64.cpp -lpthread
//客户端代码 62.cpp
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#define PORT 1234 /* Port that will be opened */
#define BACKLOG 5 /* Number of allowed connections */
#define MAXDATASIZE 4096
void process_cli(int connectfd,struct sockaddr_in client);
/* function to be executed by the new thread */
void* start_routine(void* arg);
struct ARG {
int connfd;
sockaddr_in client;
};
int main()
{
int listenfd, connectfd; /* socket descriptors */
pthread_t thread;
ARG *arg;
struct sockaddr_in server; /* server's address information */
struct sockaddr_in client; /* client's address information */
socklen_t sin_size;
/* Create TCP socket */
if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
/* handle exception */
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) {
/* handle exception */
perror("Bind error.");
exit(1);
}
if(listen(listenfd,BACKLOG) == -1){ /* calls listen() */
perror("listen() error\n");
exit(1);
}
sin_size=sizeof(struct sockaddr_in);
while(1)
{
/* Accept connection */
if ((connectfd = accept(listenfd,(struct sockaddr *)&client,&sin_size))==-1) {
perror("accept() error\n");
exit(1);
}
/* Create thread*/
arg = new ARG;
arg->connfd = connectfd;
memcpy((void *)&arg->client, &client, sizeof(client));
if (pthread_create(&thread, NULL, start_routine, (void*)arg)) {
/* handle exception */
perror("Pthread_create() error");
exit(1);
}
}
close(listenfd); /* close listenfd */
}
void process_cli(int connectfd, struct sockaddr_in client)
{
int num;
char recvbuf[MAXDATASIZE], sendbuf[MAXDATASIZE];
/* prints client's IP */
printf("%s:%d Client was connected\n",inet_ntoa(client.sin_addr), ntohs( client.sin_port) );
while (1) {
num = recv(connectfd, recvbuf, MAXDATASIZE,0);
if (num == 0) {
close(connectfd);
printf("%s:%d Client was disconnected \n",inet_ntoa(client.sin_addr), ntohs( client.sin_port) );
break;
}
if (num == -1) {
perror("recv() error\n");
break;
}
printf("recvbuf[0]:%d,recvbuf[num-1]:%d,num:%d\n",recvbuf[0],recvbuf[num-1],num);
recvbuf[num] = '\0';
printf("Received message From Client : %s\n",recvbuf);
for (int i = 0; i < num; i++) {
sendbuf[i] = recvbuf[num-i-1];
//printf("sendbuf[%d]:%d\n",i,sendbuf[i]);
}
//printf("sendbuf[0]:%d,sendbuf[num-1]:%d,num:%d\n",sendbuf[0],sendbuf[num-1],num);
send(connectfd,sendbuf,num,0); /* send to the client welcome message */
}
close(connectfd); /* close connectfd */
}
void* start_routine(void* arg)
{
ARG *info;
info = (ARG *)arg;
/* handle client’s requirement */
process_cli(info->connfd, info->client);
delete (ARG*)arg;
pthread_exit(NULL);
}