标题:linux socket connect 错误~
只看楼主
volanda
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2007-6-13
 问题点数:0 回复次数:1 
linux socket connect 错误~

刚接触socket,找了个简单的程序学习,但编译通过了,在执行服务器后返回了server socket name,然后运行客户端时出现错误:client connet: no route to host
我查了connect的错误类型,说是ICMP错误:不可路由(soft error)(目的地不可达)
我是在一台机器上即作服务器又做客户端 这个问题该如何解决呢。谢谢!!


///////server
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/select.h>

#define MAX_BUF_SIZE 1024
static char buf[MAX_BUF_SIZE];

int display_address(const char *msg, struct sockaddr_in* p_sin)
{
if(p_sin->sin_family != AF_INET)
{
fprintf(stderr, "Not an Internet socket.\n");
return -1;
}
fprintf(stdout, "%s ==> address is: %s : %d\n", msg, inet_ntoa(p_sin->sin_addr), ntohs(p_sin->sin_port));
return 0;
}

int main()
{
int sock, serv;
struct sockaddr_in sin;
int size;
fd_set set;

FD_ZERO(&set);

serv = socket(AF_INET, SOCK_STREAM, 0);
if (serv == -1)
{
perror("server socket");
return -1;
}

memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl(INADDR_ANY);
sin.sin_port = htons(1685);

if (bind(serv, (struct sockaddr *)&sin, sizeof(sin)) == -1)
{
perror("server bind");
return -1;
}

listen(serv, 5);

memset(&sin, 0, sizeof(sin));
size = sizeof(sin);
if (getsockname(serv, (struct sockaddr *)&sin, &size) == -1)
perror("serv sockname");
display_address("server socket name", &sin);
sock = accept(serv, NULL, NULL);

if (sock != -1)
{
memset(&sin, 0, sizeof(sin));
size = sizeof(sin);
if (getpeername(sock, (struct sockaddr *)&sin, &size) == -1)
perror("sock peername");
display_address("sock peer name", &sin);
memset(&sin, 0, sizeof(sin));
size = sizeof(sin);
if (getsockname(sock, (struct sockaddr *)&sin, &size))
perror("sock sockname");
display_address("sock socket name", &sin);

//sleep(1);
while(1)
{
FD_SET(sock, &set);
select(FD_SETSIZE, &set, NULL, NULL, NULL);
size = recv(sock, buf, MAX_BUF_SIZE, 0);
if (size <= 0)
break;
printf("server recv %d bytes\n", size);
}
}
//printf("server recv %d bytes\n", size);
close(serv);
close(sock);
}
////////client
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define MAX_BUF_SIZE (300)
static char buf[MAX_BUF_SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};

int main()
{
int sock;
struct sockaddr_in sin;
int size;

sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1)
{
perror("client socket");
return -1;
}

memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = inet_addr("192.168.21.207");
sin.sin_port = htons(1685);

if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) == -1)
{
perror("client connect");
return -1;
}
size = 0;
if (sock != -1)
{
size += send(sock, buf, MAX_BUF_SIZE, 0);
size += send(sock, buf, MAX_BUF_SIZE, 0);
size += send(sock, buf, MAX_BUF_SIZE, 0);
size += send(sock, buf, MAX_BUF_SIZE, 0);
size += send(sock, buf, MAX_BUF_SIZE, 0);
}
printf("client send %d bytes\n", size);
close(sock);
}

搜索更多相关主题的帖子: socket connect linux 
2007-06-13 16:17
penguinlinux
Rank: 2
等 级:新手上路
威 望:3
帖 子:18
专家分:0
注 册:2007-9-6
得分:0 
首先,加入 #include <unistd.h> ;并在close后加入return 0; 否则在编译的时候有 Warning
其次,将sin.sin_addr.s_addr = inet_addr("192.168.21.207"); 改为sin.sin_addr.s_addr = inet_addr("127.0.0.1");
即是本机的环路地址。

学linux,我得坚持住!
2007-09-07 09:21



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-147169-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.192773 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved