/*
* tcp-s.c
* Author: Alvin
* 基于TCP的服务器端代码
* 功能:服务器将客户端发来的内容倒序后发回到客户端
*
*/
#include <string.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 20
#define MYPORT 6767
#define BACKLOG 10
char recvs[MAXSIZE];
char sends[MAXSIZE];
void str_ser(int sockfd)
{
int n=0,i;
while(1)
{
memset(recvs,0,sizeof(recvs));
if((n=recv(sockfd,recvs,MAXSIZE,0))==0)
return ;
else
{
recvs[n]='\0';
printf("TCP server receiced: %s\n",recvs);
memset(sends,0,sizeof(sends));
for (i=0;i<n;i++)
sends[i]=recvs[n-1-i];
sends[n]=0;
sleep(1);
send(sockfd,sends,n,0);
printf("TCP server sent: %s\n",sends);
}
}
}
int main ( )
{
int sockfd,new_fd,numbytes,ret;
struct sockaddr_in my_addr;
struct sockaddr_in their_addr;
int sin_size,con_fd,pid;
char *buf;
/*创建监听套接口 */
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd<0)
{
printf("error in socket");
exit(1);
}
my_addr.sin_family=AF_INET;
my_addr.sin_port=htons(MYPORT);
my_addr.sin_addr.s_addr=htonl(INADDR_ANY);
bzero(&(my_addr.sin_zero),8);
/*绑定监听套接口 */
ret=bind(sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr));
if(ret<0)
{
printf("error in binding\n");
exit(1);
}
/* 监听连接请求*/
ret=listen (sockfd,BACKLOG);
if(ret<0)
{
printf("error in listening");
exit(1);
}
while(1)
{
sin_size=sizeof(their_addr);
con_fd=accept(sockfd,(struct sockaddr *)&their_addr,&sin_size);
if(con_fd<0)
{
printf("error in accept");
exit(1);
}
else
{
printf("TCP server and client connect successfully...\n");
printf(" client IP:%s\n",(char *)inet_ntoa(their_addr.sin_addr));
}
/*创建新的连接 */
if((pid=fork())==0)
/*子进程代码 */
{
close(sockfd);
str_ser(con_fd);
/*接收并处理数据 */
close(con_fd);
exit(0);
}
else
/*父进程代码 */
close(con_fd);
}
close(sockfd);
return 0;
}
|