当前位置: 技术问答>linux和unix
Socket编程connection refused问题
来源: 互联网 发布时间:2017-02-03
本文导语: 学习socket,自己试着编了一个C/S程序 代码如下: /* * * client * * */ #include #include #include #include #include #include #include #include #define SERVPORT 1234 #define MAXDATASIZE 100 int main(int argc, char *argv[]) { int sockfd, send...
学习socket,自己试着编了一个C/S程序
代码如下:
在UBUNTU防火墙关闭情况下终端1执行
终端2执行
查看端口
请问众位大神这client端运行时的connection refused是怎么回事呢?服务器的问题吗?
但是根据端口情况应该是在进行监听的。
谢谢!
代码如下:
/*
*
* client
*
* */
#include
#include
#include
#include
#include
#include
#include
#include
#define SERVPORT 1234
#define MAXDATASIZE 100
int main(int argc, char *argv[]) {
int sockfd, sendBytes;
char buf[MAXDATASIZE];
struct hostent *host;
struct sockaddr_in servAddr;
if(argc != 2) {
fprintf(stderr,"usage:./client [hostname]");
exit(1);
}
/*translate the address*/
if((host = gethostbyname(argv[1])) == NULL) {
perror("fail to get host by name");
exit(1);
}
printf("Success to get host by name...n");
/*establish a socket*/
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("fail to establish a socket");
exit(1);
}
printf("Success to establish a socket...n");
/*init sockaddr_in*/
servAddr.sin_family = AF_INET;
servAddr.sin_port = SERVPORT;
servAddr.sin_addr = *((struct in_addr *)host -> h_addr);
bzero(&(servAddr.sin_zero), 8);
/*connect the socket*/
if(connect(sockfd, (struct sockaddr *)&servAddr, sizeof(struct sockaddr)) == -1) {
perror("fail to connect the socket");
exit(1);
}
printf("Success to connect the socket...b");
/*send datas to server*/
if(sendBytes = send(sockfd, "hello", 5, 0) == -1) {
perror("fail to send datas");
exit(1);
}
printf("Success to send data!");
close(sockfd);
}
/*
*
* server
*
* */
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define SERVPORT 1234
#define BACKLOG 20
#define MAX_CON_NO 10
#define MAX_DATA_SIZE 5
int main() {
struct sockaddr_in serverSockaddr, clientSockaddr;
int sinSize, recvBytes;
int sockfd, clientfd;
char buf[MAX_DATA_SIZE];
/*establish a socket*/
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("fail to establish a socket");
exit(1);
}
printf("Success to establish a socket...(sockfd = %d)n", sockfd);
/*init sockaddr_in*/
serverSockaddr.sin_family = AF_INET;
serverSockaddr.sin_port = htons(SERVPORT);
serverSockaddr.sin_addr.s_addr = INADDR_ANY;
bzero(&(serverSockaddr.sin_zero), 8);
/*bind socket*/
if(bind(sockfd, (struct sockaddr *)&serverSockaddr, sizeof(struct sockaddr))== -1) {
perror("fail to bind");
exit(1);
}
printf("Success to bind the socket...n");
/*listen on the socket*/
if(listen(sockfd, BACKLOG) == -1) {
perror("fail to listen");
exit(1);
}
printf("Success to listen on the socket...n");
/*accept a client's request*/
if((clientfd = accept(sockfd, (struct sockaddr *)&clientSockaddr, &sinSize)) == -1) {
perror("fail to acceot");
exit(1);
}
printf("Success to accpet...n");
/*call recv to receive datas*/
if((recvBytes = recv(sockfd, buf, MAX_DATA_SIZE, 0)) == -1) {
perror("fail to receive datas");
exit(1);
}
printf("Receive a connection:%s n", buf);
close(sockfd);
}
在UBUNTU防火墙关闭情况下终端1执行
jimmy@MyPet:~$ gcc -o server server.c
jimmy@MyPet:~$ gcc -o client client.c
jimmy@MyPet:~$ ./server
Success to establish a socket...(sockfd = 3)
Success to bind the socket...
Success to listen on the socket...
终端2执行
jimmy@MyPet:~$ ./client MyPet
Success to get host by name...
Success to establish a socket...
fail to connect the socket: Connection refused
jimmy@MyPet:~$ ./client 127.0.0.1
Success to get host by name...
Success to establish a socket...
fail to connect the socket: Connection refused
jimmy@MyPet:~$ ./client 127.0.1.1
Success to get host by name...
Success to establish a socket...
fail to connect the socket: Connection refused
jimmy@MyPet:~$
查看端口
jimmy@MyPet:~$ nmap MyPet
Starting Nmap 5.21 ( http://nmap.org ) at 2012-02-19 14:35 CST
Nmap scan report for MyPet (127.0.1.1)
Host is up (0.00043s latency).
Not shown: 998 closed ports
PORT STATE SERVICE
21/tcp open ftp
1234/tcp open hotline
Nmap done: 1 IP address (1 host up) scanned in 0.19 seconds
请问众位大神这client端运行时的connection refused是怎么回事呢?服务器的问题吗?
但是根据端口情况应该是在进行监听的。
谢谢!
|
客户端的PORT没做htons
|
if(connect(sockfd, (struct sockaddr *)&servAddr, sizeof(struct sockaddr)) == -1) {
第三个参数用sizoef(struct sockaddr_in)
第三个参数用sizoef(struct sockaddr_in)