写了个socket通信的 本机调试正常 在两天机子上就不对了

bluestar2009 2012-08-19 03:20:50
本机 和两台机子都是 linux

求指教

#include<stdio.h>
#include<sys/socket.h>

//messagequeue
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<errno.h>
#include<memory.h>

#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>

#define BUFLENGTH (1024)
#define nPort (6001) //port

struct SndInfo sInfo;

struct SndInfo
{
int iType;
char sBuffer[256];
int iBufferLen;
};
struct msgbuf
{
long mtype;
char mtext[BUFLENGTH];
};

int hotMasterSndInfo(int type, char *buffer, int len)
{
sInfo.iType=type;
strcpy(sInfo.sBuffer,buffer);
printf("here sBuffer [%s]\r\n",sInfo.sBuffer);
sInfo.iBufferLen = len;
}

int main()
{
int i,ret=0;
key_t key;
int msgid;
int result;
char sIP[20]={0};
struct msgbuf data;
struct hostent *h;

int socket_connect;
struct sockaddr_in server_addr;

data.mtype = 1;
bzero(&server_addr,sizeof(server_addr));
printf("Please input target as Dotted decimal IP: \r\n");
gets(sIP);
printf("client start ...\n");


key = ftok(".",'a');
msgid = msgget(key,IPC_CREAT);
if(msgid < 0)
{
perror("messagequeue failed!\n");
exit(1);
}


while(1)
{
printf("waiting for messageQueue......\r\n");
result = msgrcv(msgid,&data,BUFLENGTH,1,0);
if (result < 0)
{
perror("recv messagequeue failed!\n");
exit(1);
}
printf("rec messagequeue OK!\r\n");


memcpy(&sInfo,data.mtext,sizeof(sInfo));
printf("main type:%d buf[%s] len:[%d]\r\n",sInfo.iType,sInfo.sBuffer,sInfo.iBufferLen);

socket_connect = socket(AF_INET,SOCK_STREAM,0);
//cilent address struct fill
server_addr.sin_family =AF_INET;
server_addr.sin_port =htons(nPort);
printf("sIP:[%s]\r\n",sIP);
server_addr.sin_addr.s_addr=inet_addr(sIP);

printf("connect ...\r\n");

if( connect( socket_connect,(struct sockaddr *)& server_addr,sizeof(server_addr) )==-1)
{
printf("connect error!\r\n");
exit(1);
}
printf("connect success!\n");
printf("send ...\r\n");
int sendSequence = 0;

int readBytes=sizeof(data.mtext);
printf("readBytes=%d\r\n",readBytes);
if(readBytes==0)
{
printf("Buffer transmit over!\r\n");
exit(1);
//break;
}
else
{
printf("Buffer [%d] bytes\r\n",readBytes);
}

int sendBytes=send(socket_connect,data.mtext,readBytes,0);

if(sendBytes<0)
{
printf("send error! \r\n");
exit(1);
}
else
{
sendSequence++;
printf("%d send to server:%d bytes\r\n",sendSequence,sendBytes);
}

if(close(socket_connect)!=0)
{
printf("close() error! \r\n");
}
else
{
printf("close socket_connect success!\r\n");
}
}
return 0;
}
...全文
116 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
WYD2608 2012-08-20
  • 打赏
  • 举报
回复
本机能通

在其他机子上不能通的原因 是因为 接收端有防火墙

设置一下 或者简单方法 关掉就OK了
赵4老师 2012-08-20
  • 打赏
  • 举报
回复
不知道有多少前人掉在TCP Socket
send(人多)send(病少)send(财富)
recv(人多病)recv(少财富)
陷阱里面啊!
http://topic.csdn.net/u/20120210/09/51109ed0-07b9-41f2-b487-a51597f2ca01.html
ocean放飞 2012-08-20
  • 打赏
  • 举报
回复
看报什么错误,把错误码打出来看看。
bluestar2009 2012-08-19
  • 打赏
  • 举报
回复
没人来么

69,381

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧