//服务器端
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MAXLINE 1000
typedef struct client{
int fd;
char *name;
struct sockaddr_in addr;
char *data;
}CLIENT;
void str_echo(CLIENT *client,char *buf,int len);
void savadata(char *buf,int len,char *data);
int main(int argc,char **argv)
{
int listenfd,connfd;
struct sockaddr_in servaddr;
char buf[MAXLINE];
CLIENT client[FD_SETSIZE];
int clilen=sizeof(struct sockaddr_in),maxfd,maxi,nready,sockfd,i,n;
fd_set rset,allset;
listenfd=socket(AF_INET,SOCK_STREAM,0);
int flag=fcntl(sockfd,F_GETFL,0);
fcntl(sockfd,F_SETFL,flag|O_NONBLOCK);
int opt=SO_REUSEADDR;
setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(13);
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
if(bind(listenfd,(struct sockaddr *)&servaddr,sizeof(servaddr))==-1)
{
perror("bind error!!!");
exit(1);
}
if(listen(listenfd,5)==-1)
{
perror("listen() error");
exit(1);
}
maxfd=listenfd;
maxi=-1;
for(int j=0;j {
client[j].fd=-1;
}
FD_ZERO(&allset);
FD_SET(listenfd,&allset);
while(1)
{
struct sockaddr_in addr;
rset=allset;
nready=select(maxfd+1,&rset,NULL,NULL,NULL);
if(FD_ISSET(listenfd,&rset))
{
//clilen=sizeof(client);
connfd=accept(listenfd,(struct sockaddr *)&addr,&clilen);
if(connfd==-1)
{
perror("accept error !!!");continue;
}
for( i=0;i<(FD_SETSIZE);i++)
{
if(client[i].fd<0)
{
client[i].fd=connfd;
client[i].addr=addr;
client[i].data=(char *)malloc(MAXLINE*sizeof(char));
client[i].name=(char*)malloc(MAXLINE*sizeof(char));
client[i].name[0]=0;
client[i].data[0]=0;
printf("you got a connection from %s:%d======================%d\n",inet_ntoa(client[i].addr.sin_addr),ntohs(client[i].addr.sin_port),i);
break;
}
}
if(i==FD_SETSIZE)
{
printf("too many client\n !!!");
}
FD_SET(connfd,&allset);
if(connfd>maxfd) maxfd=connfd;
if(i>maxi) maxi=i;
if(--nready<=0)continue;
}///end if(FD_ISSET..)
for(i=0;i<=maxi;i++)
{
if((sockfd=client[i].fd)<0)continue;
if(FD_ISSET(sockfd,&rset))
{
if((n=read(sockfd,buf,MAXLINE))==0)
{
close(sockfd);
printf("client (%s) closed connection.user's data:%s\n",client[i].name,client[i].data);
FD_CLR(sockfd,&allset);
client[i].fd=-1;
free( client[i].name);
free(client[i].data);
}
else
{
str_echo(&client[i],buf,n);
}
if(--nready<=0)break;
}
}
}
close(listenfd);
}
void str_echo(CLIENT *client,char *buf,int len)
{
char sendbuf[MAXLINE];
buf[len-1]=0;
if(strlen(client->name)==0)
{
memcpy(client->name,buf,len);
printf("client's name is %s\n",client->name);
return;
}
printf("received client (%s) message :%s \n",client->name,buf);
savadata(buf,len,client->data);
printf("%s\n",client->data);
send(client->fd,buf,strlen(buf),0);
}
void savadata(char *buf,int len,char *data)
{
int start=strlen(data);
for(int i=0;i {
data[start+i]=buf[i];
}
}
//客户端
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MAXLINE 1000
#define SIZE 1030
char * getMessage(char * sendline,int len,FILE *fp);
void process(FILE *fd,int sockfd);
int main(int argc,char **argv)
{
int sockfd,n;
char recvline[MAXLINE+1];
struct sockaddr_in servaddr;
if(argc!=2)
{
printf("usage:a.out");
exit(1);
}
if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
{
printf("erro!!\n");
}
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(13);
inet_pton(AF_INET,argv[1],&servaddr.sin_addr);
connect(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
process(stdin,sockfd);
exit(0);
}
void process(FILE *fp,int sockfd)
{
char sendline[MAXLINE],recvline[MAXLINE];
int num;
printf("connected to server.\n");
printf("input name : ");
if(fgets(sendline,MAXLINE,fp)==NULL)
{
printf("\n exit \n");
return;
}
send(sockfd,sendline,strlen(sendline),0);
while(getMessage(sendline,MAXLINE,fp)!=NULL)
{
send(sockfd,sendline,strlen(sendline),0);
if((num=recv(sockfd,recvline,MAXLINE,0))==0)
{
printf("server terminated .\n");
return;
}
recvline[num]=0;
printf("server message:%s\n",recvline);
}
printf("\n exit\n");
}
char * getMessage(char * sendline,int len,FILE *fp)
{
printf("input string to a server ");
return(fgets(sendline,MAXLINE,fp));
}