当前位置: 技术问答>linux和unix
一个用socket的聊天程序,实在是弄不出来了!
来源: 互联网 发布时间:2015-06-23
本文导语: server.c client.c 就是想实现在client端当输入"s"时就可以发送消息,server端接受到返回到client! server.c端程序如下 #include #include #include #include #include #include #include #include #include #include #define SER...
server.c client.c 就是想实现在client端当输入"s"时就可以发送消息,server端接受到返回到client!
server.c端程序如下
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define SERVPORT 3333
#define BACKLOG 10
#define MAXDATASIZE 100
main()
{
pid_t pid;
int real_len,real_send_len;
int sockfd,client_fd,numbytes;
struct sockaddr_in my_addr;
struct sockaddr_in remote_addr;
char buf[MAXDATASIZE];
int sin_size;
if((sockfd = socket(AF_INET, SOCK_STREAM, 0))==-1)
{
printf("socket error!");
exit(1);
}
my_addr.sin_family=AF_INET;
my_addr.sin_port=htons(SERVPORT);
my_addr.sin_addr.s_addr=INADDR_ANY;
bzero(&(my_addr.sin_zero),8);
if(bind(sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr))==-1)
{
printf("bind error!");
exit(1);
}
if(listen(sockfd,BACKLOG)==-1)
{
printf("listen error!");
exit(1);
}
while(1)
{
sin_size = sizeof(struct sockaddr_in);
if((client_fd = accept(sockfd,(struct sockaddr *)&remote_addr,&sin_size))==-1)
{
printf("accept error!");
}
printf("received a connection from %sn", inet_ntoa(remote_addr.sin_addr));
close(sockfd);
//if(!fork()){
//send to server
real_len = recv(client_fd,buf,MAXDATASIZE,0);
buf[real_len] = '';
printf("get from client::%s",buf);
real_send_len = send(client_fd,buf,real_len,0);
//need some process here
//}
}
close(client_fd);
}
client端程序如下
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 3333
#define DEST_IP "192.168.1.220"
#define MAXDATASIZE 100
int main(int argc,char *argv[])
{
int sockfd,numbytes,client_fd,read_len,real_send_len;
char buf[MAXDATASIZE];
//struct hostent *host;
struct sockaddr_in their_addr;
if(argc!=2)
{
fprintf(stderr,"usage: client hostnamen");
exit(1);
}
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
printf("socket error");
exit(1);
}
their_addr.sin_family=AF_INET;
their_addr.sin_port=htons(PORT);
//their_addr.sin_addr=inet_addr(DEST_IP);
their_addr.sin_addr.s_addr=inet_addr(DEST_IP);
bzero(&(their_addr.sin_zero),8);
if(connect(sockfd,(struct sockaddr *)&their_addr,sizeof(struct sockaddr))==-1)
{
printf("connect error");
exit(1);
}
while(1)
{
//send to server
read_len = recv(client_fd,buf,MAXDATASIZE,0);
buf[read_len] = '';
printf("get to server::%s",buf);
if(getchar()=='s')
{
scanf("%s",buf);
printf("send to client::%s",buf);
real_send_len = send(client_fd,buf,read_len,0);
//need some process here
}
}
close(sockfd);
return 0;
}
为了实现上面的要求!请大家帮我改改 谢谢!
server.c端程序如下
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define SERVPORT 3333
#define BACKLOG 10
#define MAXDATASIZE 100
main()
{
pid_t pid;
int real_len,real_send_len;
int sockfd,client_fd,numbytes;
struct sockaddr_in my_addr;
struct sockaddr_in remote_addr;
char buf[MAXDATASIZE];
int sin_size;
if((sockfd = socket(AF_INET, SOCK_STREAM, 0))==-1)
{
printf("socket error!");
exit(1);
}
my_addr.sin_family=AF_INET;
my_addr.sin_port=htons(SERVPORT);
my_addr.sin_addr.s_addr=INADDR_ANY;
bzero(&(my_addr.sin_zero),8);
if(bind(sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr))==-1)
{
printf("bind error!");
exit(1);
}
if(listen(sockfd,BACKLOG)==-1)
{
printf("listen error!");
exit(1);
}
while(1)
{
sin_size = sizeof(struct sockaddr_in);
if((client_fd = accept(sockfd,(struct sockaddr *)&remote_addr,&sin_size))==-1)
{
printf("accept error!");
}
printf("received a connection from %sn", inet_ntoa(remote_addr.sin_addr));
close(sockfd);
//if(!fork()){
//send to server
real_len = recv(client_fd,buf,MAXDATASIZE,0);
buf[real_len] = '';
printf("get from client::%s",buf);
real_send_len = send(client_fd,buf,real_len,0);
//need some process here
//}
}
close(client_fd);
}
client端程序如下
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 3333
#define DEST_IP "192.168.1.220"
#define MAXDATASIZE 100
int main(int argc,char *argv[])
{
int sockfd,numbytes,client_fd,read_len,real_send_len;
char buf[MAXDATASIZE];
//struct hostent *host;
struct sockaddr_in their_addr;
if(argc!=2)
{
fprintf(stderr,"usage: client hostnamen");
exit(1);
}
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
printf("socket error");
exit(1);
}
their_addr.sin_family=AF_INET;
their_addr.sin_port=htons(PORT);
//their_addr.sin_addr=inet_addr(DEST_IP);
their_addr.sin_addr.s_addr=inet_addr(DEST_IP);
bzero(&(their_addr.sin_zero),8);
if(connect(sockfd,(struct sockaddr *)&their_addr,sizeof(struct sockaddr))==-1)
{
printf("connect error");
exit(1);
}
while(1)
{
//send to server
read_len = recv(client_fd,buf,MAXDATASIZE,0);
buf[read_len] = '';
printf("get to server::%s",buf);
if(getchar()=='s')
{
scanf("%s",buf);
printf("send to client::%s",buf);
real_send_len = send(client_fd,buf,read_len,0);
//need some process here
}
}
close(sockfd);
return 0;
}
为了实现上面的要求!请大家帮我改改 谢谢!
|
http://expert.csdn.net/Expert/topic/2879/2879992.xml?temp=.5156061
看看这个先,比拟哪个要复杂!那个是我写过的!
看看这个先,比拟哪个要复杂!那个是我写过的!
|
主体流程好像没有什么问题,你还是慢慢调试一下看看吧。
|
用gdb调一下,看看程序走到哪一步?