#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define TRUE 1
#define PORT 1000
static int sockfd;
void recvfromserver() //接受服务器消息线程入口函数
{
char mes[1024];
int nbytes=0;
while(1)
{
memset(mes,0,sizeof(mes));
nbytes=read(sockfd,mes,sizeof(mes));
if(nbytes>0)
{
mes[nbytes]='\0';
printf("%s\n",mes);
}
}
pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
//int sockfd;
char buffer[1024];
struct sockaddr_in server_addr;
struct hostent *host;
int portnumber,nbytes;
char *strhost="127.0.0.1";
char clientname[20];
char password[20];
char mes[1024];
int thr_id; /* thread ID for the newly created thread */
pthread_t p_thread; /* thread's structure */
if(argc!=1)
{
fprintf(stderr,"Usage:%s \a\n",argv[0]);
exit(1);
}
if((host=gethostbyname(strhost))==NULL)
{
fprintf(stderr,"Gethostname error\n");
exit(1);
}
/* 客户程序开始建立 sockfd 描述符 */
printf("Creating the Set interface...n");
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
fprintf(stderr,"Socket Error:%s\a\n",strerror(errno));
exit(1);
}
/* 客户程序填充服务端的资料 */
bzero(&server_addr,sizeof(server_addr));
server_addr.sin_family=AF_INET;
server_addr.sin_port=htons(PORT);
server_addr.sin_addr=*((struct in_addr *)host->h_addr);
printf("The successful landing\nWelcome to zhe chat room!\n");
/* 客户程序发起连接请求 */
if(connect(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==-1)
{
fprintf(stderr,"Connect Error:%s\a\n",strerror(errno));
exit(1);
}
/* 连接成功了 */
printf("The successful landing\nWelcome to zhe chat room!\n");
printf("Please enter your nickname:\n");
scanf("%s",clientname);
printf("Please enter your password:\n");
scanf("%s",password);
printf("\nThe successful landing");
//write(sockfd,clientname,sizeof(clientname));
printf("\nNow you can chat with others!£¨\"Quit\"CUT DOWN LANDING\n\n");
thr_id = pthread_create(&p_thread, NULL, recvfromserver, NULL);
while(1)
{
memset(buffer,0,sizeof(buffer));
memset(mes,0,sizeof(mes));
scanf("%s",buffer);
strcat(mes,clientname);
strcat(mes,":");
strcat(mes,buffer);
//printf("main thread %s\n",mes);
if((write(sockfd,mes,sizeof(mes)))==-1)
{
fprintf(stderr,"Write Error:%s\n",strerror(errno));
exit(1);
}
if(strcmp(buffer,"Quit")==0)
{
break;
}
}
/* 结束通讯 */
close(sockfd);
exit(0);
}
(2)服务器代码
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define MAXLINE 1000 //在一条消息中最大的输出字符数
#define LISTENQ 20 //最大监听队列
#define PORT 1000 //监听端口
#define MAXFD 20 //最大的在线用户数量
void *get_client(void *);
int sockfd,i;
static int maxi=0;//maxi表示当前client数组中最大的用户的i值
static int client[MAXFD];
void recvandsend(void) //监听转发线程入口函数
{
int index=0;
int nbytes=0;
char buffer[1024];
int len;
int outindex=0;
while(1)
{
if(maxi>0)
{
memset(buffer,0,sizeof(buffer));
nbytes=0;
//index++;
nbytes=read(client[index++],buffer,sizeof(buffer));
//printf("%d,%d\n",index,client[index]);
if(nbytes>0)
{
buffer[nbytes]='\0';
printf(" %s\n",buffer);
outindex=0;
while(outindex<maxi)
if(write(client[outindex++],buffer,sizeof(buffer))==-1)
{
fprintf(stderr,"Write Error:%s\n",strerror(errno));
exit(1);
}
}
}
if(index>=maxi)
index=0;
}
pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
//int client_fd[LISTENQ],clientnum=0;;
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;
int sin_size,portnumber;
char hello[]="Hello! Are You Fine?\n";
int thr_id; /* thread ID for the newly created thread */
pthread_t p_thread; /* thread's structure */
int new_fd=0;
memset(client,0,sizeof(client));
if(argc!=1)
{
fprintf(stderr,"Usage:%s portnumber\a\n",argv[0]);
exit(1);
}
/* 服务器端开始建立 socket 描述符 */
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
fprintf(stderr,"Socket error:%s\n\a",strerror(errno));
exit(1);
}
/* 服务器端填充 sockaddr 结构 */
bzero(&server_addr,sizeof(struct sockaddr_in));
server_addr.sin_family=AF_INET;
server_addr.sin_addr.s_addr=htonl(INADDR_ANY);
server_addr.sin_port=htons(PORT);
/* 捆绑 sockfd 描述符 */
if(bind(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==-1)
{
fprintf(stderr,"Bind error:%s\n\a",strerror(errno));
exit(1);
}
printf("服务器监听端口%d...\n",PORT);
/* 监听 sockfd 描述符 */
if(listen(sockfd,LISTENQ)==-1)
{
fprintf(stderr,"Listen error:%s\n\a",strerror(errno));
exit(1);
}
thr_id = pthread_create(&p_thread, NULL, recvandsend, NULL);
printf("NAME:Li Junnan No:8000612030 Class:Ji ruan121\n");
printf("Welcome to the chat room!!!\n");
while(1)
{
/* 服务器阻塞,直到客户程序建立连接 */
if(maxi>=20)
{
printf("Over the max people\n");
continue;
}
sin_size=sizeof(struct sockaddr_in);
if((new_fd=accept(sockfd,(struct sockaddr *)(&client_addr),&sin_size))==-1)
{
fprintf(stderr,"Accept error:%s\n\a",strerror(errno));
exit(1);
}
/*fprintf(stderr,"Server get connection from %s\n",inet_ntoa(client_addr.sin_addr));*/
client[maxi++]=new_fd;
printf("\nNew %d user come to the chat room\n",new_fd-3);
}
close(sockfd);
exit(0);
}