linux下socket编程调试connect出错问题

Toria2015 2012-07-31 11:02:40
服务器代码socket1.c
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#define SERVPORT 2300 /*服务器监听端口号 */
#define BACKLOG 10 /*最大同时连接请求数 */
main(){
int sockfd,client_fd;
struct sockaddr_in my_addr;
struct sockaddr_in remote_addr;
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1){
perror("socket创建出错!");
exit(1);
}
my_addr.sin_family=AF_INET;
my_addr.sin_port=htons(SERVPORT);
my_addr.sin_addr.s_addr=INADDR_ANY;
bzero(&(my_addr.sin_zero),8);
if(bind(sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr))==-1){
perror("bind出错!");
exit(1);
}
if(listen(sockfd,BACKLOG)==-1)
{
perror("listen出错!");
exit(1);
}
while(1){
int sin_size=sizeof(struct sockaddr_in);
if((client_fd=accept(sockfd,(struct sockaddr *)&remote_addr,&sin_size))==-1)
{
perror("accept出错!");
continue;
}
printf("received a connection from %s",inet_ntoa(remote_addr.sin_addr));
if(!fork()){
if(send(client_fd,"Hello,you are connect!",26,0)==-1)
perror("send出错!");
close(client_fd);
exit(0);
}
close(client_fd);
}
}

客户端代码socket3.c
#include<stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define SERVPORT 2300
#define MAXDATASIZE 100 /*每次最大数据传输量 */
main(int argc, char *argv[]){
int sockfd,recvbytes;
char buf[MAXDATASIZE];
struct hostent *host;
struct sockaddr_in serv_addr;
if(argc<2){
fprintf(stderr,"Please enter the server's hostname!");
exit(1);
}
if((host=gethostbyname(argv[1]))==NULL){
perror("gethostbyname出错!");
exit(1);
}
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("socket创建出错!");
exit(1);
}
serv_addr.sin_family=AF_INET;
serv_addr.sin_port=htons(SERVPORT);
serv_addr.sin_addr=*((struct in_addr*)host->h_addr);
bzero(&(serv_addr.sin_zero),8);

if(connect(sockfd,(struct sockaddr *)serv_addr,sizeof(struct sockaddr))==-1){
perror("connect出错!");
exit(1);
}
if((recvbytes=recv(sockfd,buf,MAXDATASIZE,0))==-1){
perror("recv出错!");
exit(1);
}
buf[recvbytes]='\0';
printf("Received:%s",buf);
close(sockfd);
}

在命令行./socket1 &
再./socket3 127.0.0.1
给出connect出错!:Connection refused
...全文
300 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
luciferisnotsatan 2012-07-31
  • 打赏
  • 举报
回复
service iptables stop
Toria2015 2012-07-31
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

引用 1 楼 的回复:

把防火墙关了试试

敢问怎么关。。。
[/Quote]
我用的ubuntu12.04 貌似没有直接关闭防火墙的命令
Toria2015 2012-07-31
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

把防火墙关了试试
[/Quote]
敢问怎么关。。。
邹亚鹏 2012-07-31
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

把防火墙关了试试
[/Quote]
试试~~
IVERS0N 2012-07-31
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

把防火墙关了试试
[/Quote]

luciferisnotsatan 2012-07-31
  • 打赏
  • 举报
回复
把防火墙关了试试
Toria2015 2012-07-31
  • 打赏
  • 举报
回复
我关了,连用 的四个命令
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT
iptables -F
Toria2015 2012-07-31
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]

service iptables stop
[/Quote]
ubuntu不认识这个命令的
Toria2015 2012-07-31
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]

service iptables stop
[/Quote]
网上说这个不是红帽系统的命令吗?

69,370

社区成员

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

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