QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 1055|回复: 1

zz 简单的聊天程序...可以编译运行,但不能聊天!为什么?

[复制链接]
发表于 2006-5-26 22:02:34 | 显示全部楼层 |阅读模式
转自Linuxsir的“Linux程序设计专题讨论”,在那边没人帮楼主,只好转帖过来了

[code:1]
//////////////////server.cpp//////////////////////////////
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/unistd.h>
#include <string.h>
#include <stdio.h>
#include <pthread.h>

void *RecvMessage(void *pt);

struct tx
{
int socks;
char *buf;
};

int main()
{
int sock;
int new_sock;
char buf[100];
char bao[100];
pthread_t id;
struct tx q;
struct sockaddr_in myaddr;
sock = socket(AF_INET, SOCK_STREAM, 0);
myaddr.sin_family = AF_INET;
myaddr.sin_port = htons(8881);
myaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
bzero(&(myaddr.sin_zero),sizeof(myaddr.sin_zero));
bind(sock, (struct sockaddr *)&myaddr, sizeof(struct sockaddr));
printf("listen...\n");
listen(sock,20);
int si = sizeof(struct sockaddr);
new_sock = accept(sock,(struct sockaddr*)&myaddr,(socklen_t *)&si);
printf("connect is already! please input information:\n");
q.socks = new_sock;
q.buf = bao;
pthread_create(&id,NULL,RecvMessage,(void*)&q);
do
{
printf("please input information:\n");
scanf("%s",buf);
send(new_sock,buf,sizeof(buf),0);
}while(1);
pthread_join(id,NULL);
close(sock);
close(new_sock);
return 0;
}

void *RecvMessage(void *pt)
{
struct tx *ipt = (struct tx *)pt;
do
{
recv(ipt->socks,ipt->buf,sizeof(ipt->buf),0);
printf("%s",ipt->buf);
}while(1);
return NULL;
}

       


引用:
///////////////////client.cpp///////////////////
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/unistd.h>
#include <pthread.h>
#include <string.h>
#include <stdio.h>

void *recvMessage(void *xx);

struct posix
{
int socks;
char *bao;
};

int main()
{
int sock;
char buf[100];
char bao[100] = "";
struct posix x;
pthread_t idls;
struct sockaddr_in myaddr;
sock = socket(AF_INET, SOCK_STREAM, 0);
myaddr.sin_family = AF_INET;
myaddr.sin_port = htons(8881);
myaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
bzero(&(myaddr.sin_zero),8);
connect(sock,(struct sockaddr*)&myaddr,sizeof(struct sockaddr));
x.socks = sock;
x.bao = bao;
pthread_create(&idls,NULL,recvMessage,(void *)&x);
do
{
printf("please input information:\n");
scanf("%s",buf);
send(sock,buf,sizeof(buf),0);
}while(1);
return 0;
}

void *recvMessage(void *xx)
{
struct posix *tt = (struct posix*)xx;
do
{
recv(tt->socks,tt->bao,sizeof(tt->bao),0);
printf("he saids",tt->bao);
}while(1);
return NULL;
}
[/code:1]       


简单的程序!
只要懂socket套接字基础和pthread多线程基础,就可以看懂.
实现:客户端与服务端的聊天.
编译是没问题了,可是就是不能聊天...(前几天没加入多线程还可以实现简单的
s发消息,c接消息,c发消息,s接消息,s发消息,c接消息,c发消息 ...(有规律的)
为了实现任何一方可以随时发消息,就加入了多线程,(把recv放到了单独线程中)可是竟成现在这样了...
一方也发不了,一方也收不了..........
希望哪位仁兄能帮助一下.
问题在何处?真心需要得到帮助...
如果这里说不清楚可以加
msn:[email protected]
QQ:290078300
//天天都在线...(下午2点-夜里3点)
等待回复或者加我MSN || QQ
发表于 2006-5-27 08:16:10 | 显示全部楼层
为什么不在SERVER端使用select()
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-11-2 14:32 , Processed in 0.034941 second(s), 16 queries .

© 2021 Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表