socket编程的问题,为什么运行失败呢?

liuyang1943 2010-04-30 04:03:41
server端:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <memory>
#include <arpa/inet.h>
#include "error.h"
int main()
{
int sockfd,client_fd;/*sockfd: monitor socket; client_fd:data transmission socket*/
struct sockaddr_in my_addr;/*local host address message*/
struct sockaddr_in remote_addr;/*client address message*/
int ret;
do
{
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
ret=SOCKER_GREAT_ERROR;
break;
}
my_addr.sin_family=AF_INET;
my_addr.sin_port=htons(SERVERPORT);
my_addr.sin_addr.s_addr = INADDR_ANY;
memset(my_addr.sin_zero,0,sizeof(my_addr.sin_zero));

if(bind(sockfd,(struct sockaddr*)&my_addr,sizeof(struct sockaddr))==-1)
{
ret=BIND_ERROR;
break;
}

if(listen(sockfd,BACKLOG)==-1)
{
ret=LISTEN_ERROR;
break;
}

while(1)
{
socklen_t sin_size=sizeof(struct sockaddr);
if((client_fd=accept(sockfd,(struct sockaddr*)&remote_addr,&sin_size))==-1)
{
ret=ACCEPT_ERROR;
break;
}

printf("received a connection from %s\n", inet_ntoa(remote_addr.sin_addr));
if (!fork())
{
if (send(client_fd, "Hello, you are connected!\n", 26, 0) == -1)
{
ret=SEND_ERROR;
}
close(client_fd);
exit(0);
}

}

}while(0);
if(0!=ret)
printf("run error with %d\n",ret);
return ret;
}

client端:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <memory>
#include <arpa/inet.h>
#include "error.h"
#include <netdb.h>

int main(int argc,char *argv[])
{
int ret;
do
{
int sockfd,recvdytes;
char buf[MAXDATASIZE];
hostent *host;
struct sockaddr_in serv_add;
if (argc < 2)
{
printf("Please enter the server's hostname!\n");
ret=ERROR_PARA;
break;
}
if((host=gethostbyname(argv[1]))=NULL)
{
ret=ERROR_GETHOSTNAME;
break;
}

if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
ret=SOCKER_GREAT_ERROR;
break;

}

serv_add.sin_family=AF_INET;
serv_add.sin_port=htons(SERVERPORT);
serv_add.sin_addr = *((struct in_addr *)host->h_addr);
memset(serv_add.sin_zero,0,sizeof(serv_add.sin_zero));

if(connect(sockfd,(struct sockaddr*)&serv_add,sizeof(struct sockaddr))==-1)
{
ret=CONNECT_ERROR;
break;
}
if(recvdytes=recv(sockfd,buf,MAXDATASIZE,0)==-1)
{
ret=RECV_ERROR;
break;


}
buf[recvdytes]='\0';
printf("recvytes=%s",buf);
close(sockfd);

}while(0);

printf("run error with %d\n",ret);
return ret;
}


为什么运行client端程序时会报内存错误呢?运行方式为 ./client <hostname>
有什么问题吗?
...全文
154 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
cattycat 2010-05-04
  • 打赏
  • 举报
回复
linux不用WSAStartup,看看你的connect失败的话,看是不是获取的主机地址不对呢。
serverpot常量在哪定义的。
acrobatyuer 2010-05-04
  • 打赏
  • 举报
回复
这个在LINUX下面不太了解情况哦!~~~不过在WINDOWS下的话需要在用之前调用WSAStartup这个接口去初始化SOCKET的。
liuyang1943 2010-05-04
  • 打赏
  • 举报
回复
顶。。。
liuyang1943 2010-05-04
  • 打赏
  • 举报
回复
恩,检查了下,ret忘了赋值为0,将ret赋值0后最后客户端显示信息为:recvytes=
也就是说并没有收到服务器端发送过来的字符串,问题出在哪里呢?
liuyang1943 2010-05-04
  • 打赏
  • 举报
回复
我在服务器端: ./server
客户端:./client 127.0.0.1
然后在服务器端得到:received a connection frpm 127.0.0.1
客户端得到:recvytes=run error with 134514358
客户端跟服务端程序都在主机上运行的,为什么会这样呢
wesleyluo 2010-05-04
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 liuyang1943 的回复:]
才发觉error.h文件中错误定义重复了,改了下,发觉报错其实是gethostbyname()失败,为什么会失败呢,我是在Linux环境下测试的。。
[/Quote]
你的hostname 正确吗?
liuyang1943 2010-05-04
  • 打赏
  • 举报
回复
才发觉error.h文件中错误定义重复了,改了下,发觉报错其实是gethostbyname()失败,为什么会失败呢,我是在Linux环境下测试的。。
liuyang1943 2010-05-04
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 cattycat 的回复:]
linux不用WSAStartup,看看你的connect失败的话,看是不是获取的主机地址不对呢。
serverpot常量在哪定义的。
[/Quote]


#define MAXDATASIZE 100
#define ERROR_PARA 1000
#define ERROR_GETHOSTNAME 1006
#define SERVERPORT 3333 /*服务器监听端口号*/
#define BACKLOG 10 /*max connet number*/
#define BIND_ERROR 1001
#define LISTEN_ERROR 1002
#define ACCEPT_ERROR 1003
#define SEND_ERROR 1004
#define SOCKER_GREAT_ERROR 1005
#define CONNECT_ERROR 1006
#define RECV_ERROR 1007
liuyang1943 2010-04-30
  • 打赏
  • 举报
回复
resource temporarily unavailable

报错信息如上,为什么会这样呢,是不是我运行client程序时参数不对?
我直接带的hostname的名字,有问题么?
liuyang1943 2010-04-30
  • 打赏
  • 举报
回复
errno==11
知道错误,可我还是不明白错误时怎么发生的啊。。。
aizibion 2010-04-30
  • 打赏
  • 举报
回复
发生错误,打印errno这个变量值,再对照错误码表进行定位。
liuyang1943 2010-04-30
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 ylke2007 的回复:]
if((host=gethostbyname(argv[1]))=NULL)
编码归范呀,
if((host=gethostbyname(argv[1]))==NULL)
[/Quote]

汗。。。大意了,改了之后能运行,但是却会报connect error的错误,我是在linux下测试的,server端跟client端都在同一台主机上为什么会出现这个问题呢?
赵4老师 2010-04-30
  • 打赏
  • 举报
回复
Windows下需要初始化
The following code fragment demonstrates how an application that supports only version 2.2 of Windows Sockets makes a WSAStartup call:

WORD wVersionRequested;
WSADATA wsaData;
int err;

wVersionRequested = MAKEWORD( 2, 2 );

err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
return;
}

/* Confirm that the WinSock DLL supports 2.2.*/
/* Note that if the DLL supports versions greater */
/* than 2.2 in addition to 2.2, it will still return */
/* 2.2 in wVersion since that is the version we */
/* requested. */

if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 2 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
WSACleanup( );
return;
}

/* The WinSock DLL is acceptable. Proceed. */

ylke2007 2010-04-30
  • 打赏
  • 举报
回复
if((host=gethostbyname(argv[1]))=NULL)
编码归范呀,
if((host=gethostbyname(argv[1]))==NULL)

向立天 2010-04-30
  • 打赏
  • 举报
回复
好像没看见你初始化

64,670

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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