当前位置: 技术问答>linux和unix
SOCKET聊天室程序求助(有源程序)100分
来源: 互联网 发布时间:2015-09-28
本文导语: 上次的TALK程序老师说内容太少了,只好再换 从书上抄的聊天室程序,C/S模式,好象缺了个config文件,而书上又没说在哪 给一百分,希望能得到调试成功后的程序 /*inet.h*/ #ifndef __INET_H__ #define __INET_H__ #include #include...
上次的TALK程序老师说内容太少了,只好再换
从书上抄的聊天室程序,C/S模式,好象缺了个config文件,而书上又没说在哪
给一百分,希望能得到调试成功后的程序
/*inet.h*/
#ifndef __INET_H__
#define __INET_H__
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MAX_CLIENT 10
#define READ 0
#define WRITE 1
#define MAX_LINE 1000
#define MAX_NAME 100
#define SETNAME "/name"
#define __SELECT__
#endif
客户端
#include "inet.h"
#include
#include
extern int init_cli();
void add_set( fd_set *sockset, int sockfd)
{
FD_ZERO( sockset);
FD_SET( sockfd, sockset);
FD_SET( 0, sockset);
}
int main( void)
{
int sockfd;
int status;
char str[MAX_LINE];
char name[MAX_NAME];
fd_set sockset;
sockfd = init_cli();
if ( sockfd == 0) {
printf( "Init client socket error.n");
fflush( stdout);
exit( 1);
}
add_set( &sockset, sockfd);
fprintf( stdout, "Please input your name:");
fscanf( stdin, "%s", name);
strcpy( str, "/");
strcat( str, name);
write( sockfd, str, strlen( str));
while ( 1) {
select( sockfd+1, &sockset, NULL, NULL, NULL);
if (FD_ISSET( sockfd, &sockset)) {
status = read( sockfd, str, MAX_LINE);
if ( status == 0) exit(0);
str[status] = '';
printf( "%s", str);
fflush( stdout);
}
if ( FD_ISSET( 0, &sockset)) {
status = read( 0, str, MAX_LINE);
str[status] = '';
if ( str[0] == 'q') {
sprintf( str, "%s leave the room.n", name);
write( sockfd, str, strlen( str));
close( sockfd);
exit( 0);
}
if ( write( sockfd, str, strlen(str)) != strlen( str) ) {
printf( "Write errorn");
exit(0);
}
}
add_set( &sockset, sockfd);
}
return 1;
}
int init_cli( void)
// Return sockfd if successful, else 0
{
int sockfd;
int SERV_TCP_PORT;
char SERV_HOST_ADDR[MAX_LINE];
FILE *fd;
struct sockaddr_in serv_addr;
fd = fopen( "config", "r");
fgets( SERV_HOST_ADDR, MAX_LINE, fd);
fscanf( fd, "%d", &SERV_TCP_PORT);
fclose( fd);
bzero( (char*)&serv_addr, sizeof( serv_addr));
serv_addr.sin_family =AF_INET;
serv_addr.sin_addr.s_addr =inet_addr( SERV_HOST_ADDR);
serv_addr.sin_port =htons( SERV_TCP_PORT);
if ( ( sockfd = socket( AF_INET,SOCK_STREAM,0))
从书上抄的聊天室程序,C/S模式,好象缺了个config文件,而书上又没说在哪
给一百分,希望能得到调试成功后的程序
/*inet.h*/
#ifndef __INET_H__
#define __INET_H__
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MAX_CLIENT 10
#define READ 0
#define WRITE 1
#define MAX_LINE 1000
#define MAX_NAME 100
#define SETNAME "/name"
#define __SELECT__
#endif
客户端
#include "inet.h"
#include
#include
extern int init_cli();
void add_set( fd_set *sockset, int sockfd)
{
FD_ZERO( sockset);
FD_SET( sockfd, sockset);
FD_SET( 0, sockset);
}
int main( void)
{
int sockfd;
int status;
char str[MAX_LINE];
char name[MAX_NAME];
fd_set sockset;
sockfd = init_cli();
if ( sockfd == 0) {
printf( "Init client socket error.n");
fflush( stdout);
exit( 1);
}
add_set( &sockset, sockfd);
fprintf( stdout, "Please input your name:");
fscanf( stdin, "%s", name);
strcpy( str, "/");
strcat( str, name);
write( sockfd, str, strlen( str));
while ( 1) {
select( sockfd+1, &sockset, NULL, NULL, NULL);
if (FD_ISSET( sockfd, &sockset)) {
status = read( sockfd, str, MAX_LINE);
if ( status == 0) exit(0);
str[status] = '';
printf( "%s", str);
fflush( stdout);
}
if ( FD_ISSET( 0, &sockset)) {
status = read( 0, str, MAX_LINE);
str[status] = '';
if ( str[0] == 'q') {
sprintf( str, "%s leave the room.n", name);
write( sockfd, str, strlen( str));
close( sockfd);
exit( 0);
}
if ( write( sockfd, str, strlen(str)) != strlen( str) ) {
printf( "Write errorn");
exit(0);
}
}
add_set( &sockset, sockfd);
}
return 1;
}
int init_cli( void)
// Return sockfd if successful, else 0
{
int sockfd;
int SERV_TCP_PORT;
char SERV_HOST_ADDR[MAX_LINE];
FILE *fd;
struct sockaddr_in serv_addr;
fd = fopen( "config", "r");
fgets( SERV_HOST_ADDR, MAX_LINE, fd);
fscanf( fd, "%d", &SERV_TCP_PORT);
fclose( fd);
bzero( (char*)&serv_addr, sizeof( serv_addr));
serv_addr.sin_family =AF_INET;
serv_addr.sin_addr.s_addr =inet_addr( SERV_HOST_ADDR);
serv_addr.sin_port =htons( SERV_TCP_PORT);
if ( ( sockfd = socket( AF_INET,SOCK_STREAM,0))