| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 290 人关注过本帖
标题:小问题求助
只看楼主 加入收藏
zux51046
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2007-10-22
收藏
 问题点数:0 回复次数:1 
小问题求助

小第请大哥大姐帮忙看看程序,要求用TCP编一个聊天程序,比如A登陆后把他输入的内容用sock传给其他以登陆的人,程序如下,可是执行后有点错误,我估计是tcp client.c的错误,请大家帮忙看看。要不我就交不了作业了。先谢谢大家了。

inet.h

#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>

#define SERVER_ADDR "130.153.178.163"
#define STUDENT_ID 046
#define PORT_BASE 12000
#define SERVER_PORT (PORT_BASE+STUDENT_ID)

tcp server.c

#include"inet.h"
#define BUFSIZE 1024
#define NAMELEN 64
#define MAX_NUMBER 10

char *message1="input your name.\n";
char *message2="logined success.\n";

typedef struct{
int fd;
int flag;
char name[NAMELEN];
struct sockaddr_in cli;
} MEMBER;
MEMBER member[MAX_NUMBER];

void error(char *s){
perror(s);
exit(1);
}

int server(int fd,struct sockaddr_in cli,fd_set allfds){
char b[BUFSIZE];
int n;
char *ipaddr;
int wfd;
int clilen;
char *str="-->";
fd_set writefds=allfds;
if(member[fd].flag==1){
n=login(fd,writefds);
}else{
// ipaddr=inet_ntoa(cli.sin_addr);
if((n=read(fd,b,BUFSIZE))<=0) return 0;
b[n]='\0';
printf("server fd=%d\n",fd);
// printf("From ipaddr:%s\n",ipaddr);
printf("--------------------------------");
printf("From %s --> %s",member[fd].name,b);

if(select(FD_SETSIZE,NULL,&writefds,NULL,NULL)<0)
error("cannot select");
for(wfd=0;wfd<FD_SETSIZE;wfd++){
if(FD_ISSET(wfd,&writefds)){
if(wfd!=fd){
printf("To %s-->",member[wfd].name);
printf("%s %s",member[fd].name,"logouted");
write(wfd,member[fd].name,strlen(member[fd].name));
write(wfd,"logouted\n",10);
}
else{
printf("To %s--> %s",member[wfd].name,b);
write(wfd,member[fd].name,strlen(member[fd].name));
write(wfd,str,strlen(str));
write(wfd,b,n);
}
}
}
printf("-------------------------------\n");
}
return n;
}

int login(int fd,fd_set allfds){
int n;
member[fd].fd=fd;

if(select(FD_SETSIZE,&allfds,NULL,NULL,NULL)<0)
error("cannot select");
if(FD_ISSET(fd,&allfds)){
if((n=read(fd,member[fd].name,NAMELEN))>0&&n!=1){
write(fd,message2,strlen(message2));
printf("login success from %s",member[fd].name);
member[fd].name[n-1]='\0';
member[fd].flag=0;
}
else{
write(fd,message1,strlen(message1));
}
}
return n;
}

int main(int argc,char **argv){
int sockfd,newfd;
struct sockaddr_in sin,cli;
fd_set allfds,fds;
int fd;
int clilen;
int pid;
int count=0;

if((sockfd=socket(PF_INET,SOCK_STREAM,0))<0)
error("canot create socket");
bzero((char *)&sin,sizeof(sin));
sin.sin_family=PF_INET;
sin.sin_addr.s_addr=htonl(INADDR_ANY);
sin.sin_port=htons(SERVER_PORT);
if(bind(sockfd,(struct sockaddr *)&sin,sizeof(sin))<0)
error("cannot bind");
if(listen(sockfd,5)<0)
error("cannot listen");
FD_ZERO(&allfds);
FD_SET(sockfd,&allfds);
clilen=sizeof(cli);
while(1){
fds=allfds;
if(select(FD_SETSIZE,&fds,NULL,NULL,NULL)<0)
error("cannot select");
for(fd=0;fd<FD_SETSIZE;fd++)
if(FD_ISSET(fd,&fds)){
if(fd==sockfd){
clilen=sizeof(cli);
if((newfd=accept(sockfd,(struct sockaddr *)&cli,&clilen))<0)
error("cannot accept");
write(newfd,message1,strlen(message1));
member[newfd].flag=1;
member[newfd].cli=cli;
FD_SET(newfd,&allfds);
}else if(server(fd,member[fd].cli,allfds)==0){
printf("Closed connection: fd=%d\n",fd);
FD_CLR(fd,&allfds);close(fd);


}
}
}
}


tcp client.c

#include"inet.h"
#define BUFSIZE 1024
static int sockfd;

void error(char *s){
perror(s);
exit(1);
}

void client(int fd){
char b[BUFSIZE];
int n;
fd_set fds;
int flag=1;
int i;
int len=0;
int c;
while(1){
FD_ZERO(&fds);
FD_SET(0,&fds);
FD_SET(fd,&fds);
if(select(FD_SETSIZE,&fds,NULL,NULL,NULL)<0)
error("cannot select");
if(FD_ISSET(0,&fds)){
c=getchar();
if(c=='\n'||c=='\r'){
b[len++]='\n';

if(write(fd,b,len)<=0)
return;
len=0;
}
else{
b[len++]=c;
}

}
if(FD_ISSET(fd,&fds)){
if((n=read(fd,b,BUFSIZE))<=0)
return;

}
}
}

int main(int argc,char **argv){
int sockfd,n;
struct sockaddr_in sin;

if((sockfd=socket(PF_INET,SOCK_STREAM,0))<0)
error("canot create socket");
bzero((char *)&sin,sizeof(sin));
sin.sin_family=PF_INET;
sin.sin_addr.s_addr=inet_addr(SERVER_ADDR);
sin.sin_port=htons(SERVER_PORT);

// n=connect(sockfd,(struct sockaddr *)&sin,sizeof(sin));
// printf("n=%d\n",n);
if(connect(sockfd,(struct sockaddr *)&sin,sizeof(sin))<0)
error("cannot connect");

client(sockfd);
close(sockfd);
exit(0);
}


搜索更多相关主题的帖子: 大哥 聊天 include 
2007-10-22 20:13
zux51046
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2007-10-22
收藏
得分:0 
回复:(zux51046)小问题求助
2007-10-22 20:54
快速回复:小问题求助
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016769 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved