当前位置:文档之家› 设计聊天室 实验报告

设计聊天室 实验报告

{
printf( "Init sever socket error\n");
fflush( stdout);
//exit(1);
} //Socket init done
listen( sockfd, MAX_CLIENT);
cli_len = sizeof( cli_addr);
for ( i = 0; i<MAX_CLIENT; i++)
{
if ( (j != i)&&(user_link[j]==1))
{
write( userfd[j], line, strlen( line));
}
}
}
else if(line[0]=='l'&&line[1]=='i'&&line[2]=='s'&&line[3]=='t')
{
bzero(line,MAX_LINE);
strcat( str, name);
write( sockfd, str, strlen( str));
fcntl( 0, F_SETFL, O_NONBLOCK);
while ( 1) {
if ( (status = read( sockfd, str, MAX_LINE))>=0 )
{
if ( status == 0) exit(0);
username[i][0] = '\0';
}
else if ( length >0) {
line[length] = '\0';
if ( (line[0] == '/')&&(username[i][0] == '\0'))
{
set_name( line, username[i]);
for ( j=0; j<MAX_CLIENT;j++)
if ( ( sockfd = socket( AF_INET,SOCK_STREAM,0))<0) {
perror( "socket:");
printf( "server:can`t open stream socker.\n");
fflush( stdout);
return( 0);
}
bzero( ( char*)&serv_addr, sizeof( serv_addr));
}
intuser_free( int user_link[MAX_CLIENT])
{
inti = 0;
while ( ( user_link[i] != 0)&&( i<MAX_CLIENT)) i++;
if ( i == MAX_CLIENT) return( -1);
return( i);
}
int main( void)
sizeof( serv_addr))<0) {
perror( "bind:");
printf( "server: can`t bind local address\n");
fflush( stdout);
return( 0);
}
return( sockfd); //successful.
}
客户端部分:
{
user_link[i] = 0;
username[i][0] = '\0';
}
userCount = 0;
fcntl( sockfd, F_SETFL, O_NONBLOCK);
for ( ; ;) {
if ( (userCount = user_free( user_link)) >=0)
{
new_sockfd = accept( sockfd, (struct sockaddr*)&cli_addr,&cli_len);
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr =htonl( INADDR_ANY);
serv_addr.sin_port=htons( SERV_TCP_PORT);
if ( bind( sockfd, ( struct sockaddr *)&serv_addr,
fflush( stdout);
exit( 1);
}
fcntl( sockfd, F_SETFL, O_NONBLOCK);
fprintf( stdout, "Please input your name:");
fscanf( stdin, "%s", name);
strcpy( str, "/");
3.对于输出用户功能,客户端输入“list”,服务器通过比较给客户端传输全部的用户。
4.对于私聊功能,通过“*”符号进行判断,利用指针取出“*”之后的名字,只对该名字进行转发。
servse.c服务器端代码
#include <signal.h>
#include <sys/wait.h>
#include <netinet/in.h>
int mainus;
charstr[MAX_LINE];
char name[MAX_NAME];
sockfd = init_cli();
if ( sockfd == 0) {
printf( "Init client socket error.\n");
file = fopen( "config", "r");
fgets( line, MAX_LINE, file);
fscanf( file, "%d", &port);
fclose( file);
printf ( "%d \n", port);
sockfd = init_ser( port);
if ( sockfd == 0)
for ( i=0; i<MAX_CLIENT;i++) {
if ( user_link[ i] == 1) {
length = read( userfd[i], line, MAX_LINE);
if ( length == 0) // socket is closed.
{
user_link[i] = 0;
for ( j=0; j<MAX_CLIENT;j++)
{
if ( (j != i)&&(user_link[j]==1))
{
write( userfd[j], line, strlen( line));
}
}
}
} // length >0
} // user_link[i] == 1
} // for
} // for
1.手动输入服务器端IP地址和端口号进行连接
2.发送消息给服务器端并显示服务器端回传的消息
3.监控连接状态,客户离开或故障时从列表中删除相应表项,并及时更新连接表
client.c客户端代码:
#include <stdio.h>
#include<string.h>
#include<stdlib.h>
#include <errno.h>
fcntl( new_sockfd, F_SETFL, O_NONBLOCK);
if ( new_sockfd < 0)
{
user_link[userCount] = 0;
}
else
{
user_link[userCount] = 1;
userfd[userCount] = new_sockfd;
}
} // if userCount >= 0
else if(line[0] == '*')
{
ps=cmpstr;
pt=line;
pr=linestr;
pt++;
while((*pt)!=':')
{
*ps=*pt;
ps++;
pt++;
}
*ps='\0';
pt++;
while((*pt)!='\0')
{
*pr=*pt;
pr++;
pt++;
}
*pr='\0';
for(k=0;k<MAX_CLIENT;k++)
{
if(user_link[k]==1)
{
strcat(line,username[k]);
strcat(line," ");
}
}
// strcat(line,'\0');
write( userfd[i], line, strlen(line));
}
}
void add_name(char *line, char *name)
{
char theline[MAX_LINE];
strcpy( theline, name);
相关主题