一个网络编程方面的问题,connect()老是报错,请高手帮忙!
客户端程序每次都提示下面的错误,不知道是什么原因:
-> socket_client
addr: 192.168.10.34
create socket OK!!!!!!!
: OK
connect error: S_errno_EHOSTUNREACH
value = -1 = 0xffffffff
下面是客户端的代码:
#include <vxWorks.h>
#include <string.h>
#include <errnoLib.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sockLib.h>
#include <ioLib.h>
#include <stdio.h>
#include <hostLib.h>
#include <inet.h>
#defineport_num6100
int socket_client()
{
int sockAddrSize; /* size of socket address structure */
int clientsocket;
struct sockaddr_in serversocket;/* connector's address information */
char buf[200];
int numbytes;
struct hostent *he;
char *server = "192.168.10.34";
sockAddrSize = sizeof (struct sockaddr_in);
bzero ((char *) &serversocket, sockAddrSize);
serversocket.sin_family = AF_INET; /* host byte order */
serversocket.sin_port = htons(port_num); /* short, network byte order */
serversocket.sin_len = (u_char) sockAddrSize;
serversocket.sin_addr.s_addr = inet_addr(server);
bzero((char *)&(serversocket.sin_zero),8);
printf("addr: %s\n",inet_ntoa(serversocket.sin_addr));
if((clientsocket = socket(AF_INET,SOCK_STREAM,0))==ERROR)
{
perror("create socket error\n");
return(ERROR);
}
perror("create socket OK!!!!!!!\n");
if((connect(clientsocket,(struct sockaddr*)&serversocket,sockAddrSize))==ERROR)
{
perror("connect error");
return(ERROR);
}
perror("connect OK\n");
if((numbytes = recv(clientsocket, buf,200,0)))
{
perror("recv error");
return(ERROR);
}
perror("ALL OK!!!!");
buf[numbytes]='\0';
printf("Received: %s",buf);
close(clientsocket);
return 0;
}
下面是服务器端的代码:
#include <vxWorks.h>
#include <string.h>
#include <errnoLib.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sockLib.h>
#include <ioLib.h>
#include <stdio.h>
#include <inet.h>
#defineport_num6100
/*#define Client_IP"192.168.10.34"*/
int socket_service()
{
struct sockaddr_in serverAddr; /* server's socket address */
struct sockaddr_in clientAddr; /* client's socket address */
int sockAddrSize; /* size of socket address structure */
int mysocket; /* socket file descriptor */
int sClientSocket;
/* set up the local address */
sockAddrSize = sizeof (struct sockaddr_in);
bzero ((char *) &serverAddr, sockAddrSize);
serverAddr.sin_family = AF_INET;
serverAddr.sin_len = (u_char) sockAddrSize;
serverAddr.sin_port = htons (port_num);
serverAddr.sin_addr.s_addr = htonl (INADDR_ANY);
/*CREATE a socket*/
if((mysocket = socket(AF_INET,SOCK_STREAM,0))==ERROR)
{
perror("create socket error\n");
return(ERROR);
}
perror("create socket OK!!!!!!!!\n");
/*bind socket to server address*/
if(bind(mysocket,(struct sockaddr*)&serverAddr,sockAddrSize) == ERROR)
{
perror("bind error\n");
close(mysocket);
return(ERROR);
}
perror("bind OK!!!!!!!!!\n");
/*listen */
if(listen(mysocket,5) == ERROR)
{
perror("ERROR\n");
return(ERROR);
}
perror("LISTEN OK!!!!\n");
/*ACCEPT() wait for new client connection */
while(1)
{
sockAddrSize = sizeof (struct sockaddr_in);
if((sClientSocket==accept(mysocket,(struct sockaddr *) &clientAddr, &sockAddrSize))==ERROR)
{
perror (" socket accept error\n");
continue; /*不执行下面语句直接进行下次循环*/
}
printf("server: got connection from %s\n", inet_ntoa(clientAddr.sin_addr));
if ((send(sClientSocket, "connected!!\n", 14, 0)) == ERROR)
perror("send error");
close(sClientSocket);
break;
}
perror("ALL OK!!!!!\n");
return OK;
}
服务器端运行结果显示如下:
->
-> socket_service
create socket OK!!!!!!!!
: OK
bind OK!!!!!!!!!
: OK
LISTEN OK!!!!
: OK
请各位帮我看看是什么地方出问题了,先谢谢大家。
[解决办法]
你的错误提示主机不可达,检查一下主机能否ping通.
[解决办法]
我的意思是链接的对端机器不可达,不是你的主机。
[解决办法]