我已经为linux中的套接字通信编写了一个基本的客户端和服务器程序 服务器代码:
#include<stdio.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<string.h>
void error(char * msg){ perror(msg); exit(1); }
int main(int argc, char *argv[]){
int sockfd,listenfd,portno,clilen,n; char buffer[256]; struct sockaddr_in serv_addr, cli_addr;
if (argc<2) error("error port number not provided");
sockfd = socket(AF_INET, SOCK_STREAM, 0); if(sockfd<0) error("error while creating socket "); bzero((char*) &serv_addr, sizeof(serv_addr)); portno = atoi(argv[1]); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port
= htons(portno);
if(bind(sockfd,(struct sockaddr*)&serv_addr,sizeof serv_addr)<0) error("error while binding socket"); listen(sockfd,5); clilen = sizeof(cli_addr);
if ((listenfd = accept(sockfd, (struct sockaddr*)&cli_addr,&clilen))<0) error("error while initializing listening"); printf("listening for connections.."); while(strncmp(buffer,"bye",3)!=0){ bzero(buffer,256); //n= read(listenfd,buffer,255); n= recv(listenfd,buffer,255,0); if(n<0) error("no message"); printf("\nfrd:%s",buffer); printf("\nyou:"); fgets(buffer,255,stdin); //n= write(listenfd,buffer,sizeof buffer); n=send(listenfd,(char*)&buffer,strlen(buffer),0); if(n<0) printf("message not sent");
} close(sockfd); }
客户代码:
#include<stdio.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<string.h>
void error(char * msg){ perror(msg); exit(1); }
int main(int argc, char *argv[]){
int sockfd,portno,n; char buffer[256]; struct sockaddr_in serv_addr; struct hostent *server;
if (argc<3) error("error port number not provided");
portno = atoi(argv[2]); sockfd = socket(AF_INET, SOCK_STREAM, 0); if(sockfd<0) error("error while creating socket "); server
=(struct hostent *)gethostbyname(argv[1]); if(server == NULL){ fprintf(stderr,"ERROR, no such host\n"); exit(0); }
bzero((char *) &serv_addr, sizeof(serv_addr)); serv_addr.sin_family
= AF_INET; //bcopy((char *)server->h_addr,(char *)&serv_addr.sin_addr.s_addr, server->h_length); serv_addr.sin_port=htons(portno);
if(connect(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr))<0) error("error while connecting.."); while(strncpy(buffer,"bye",3)!=0){ bzero(buffer,256); printf("\nYou:"); fgets(buffer,255,stdin);
//n= write(sockfd,buffer,strlen(buffer)); n=send(sockfd,(char*)&buffer,strlen(buffer),0); if(n<0) printf("message not delivered\n"); bzero(buffer,256); //n= read(sockfd,buffer,255); n= recv(sockfd,buffer,255,0); printf("\nfrd:%s",buffer); }
close(sockfd); }
当我在不同终端的系统上运行这些程序时,它运行得非常好,我能够发送和重新发送消息
服务器终端
h2o @ h2o-Vostro-1015:〜/ C $ ./hserver 5400正在侦听连接.. FRD:喜
在:你好
frd:我认为它的工作
你:是的
客户终端
h2o @ h2o-Vostro-1015:〜/ C $ ./hclient localhost 5400
您:喜
FRD:喂
你:我认为它的工作
frd:是的,
您:
但是当我尝试在连接到互联网的不同机器上运行客户端代码和服务器代码时,启动服务器后,当我在运行服务器代码的机器的ip作为参数的不同机器上启动客户端代码时,我无法建立连接,错误连接被拒绝。
正在路由器后面导致ip问题..或者有一些我缺少的东西..? 请建议如何在不同的机器上运行客户端和服务器。
输出iptables -L命令
`root@h2o-Vostro-1015:~# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination`
输出ifconfig,无线局域网连接显示为eth1
h2o@h2o-Vostro-1015:~$ ifconfig
eth0 Link encap:Ethernet HWaddr 00:26:b9:01:33:9c
UP BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:25 errors:0 dropped:0 overruns:0 frame:0
TX packets:312 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1620 (1.6 KB) TX bytes:27565 (27.5 KB)
eth1 Link encap:Ethernet HWaddr 0c:60:76:4d:ed:31
inet addr:172.16.52.71 Bcast:172.16.55.255 Mask:255.255.252.0
inet6 addr: fe80::e60:76ff:fe4d:ed31/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:172420 errors:2 dropped:0 overruns:0 frame:2451108
TX packets:62286 errors:31 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:62717146 (62.7 MB) TX bytes:7900633 (7.9 MB)
Interrupt:17 Base address:0xc000
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:6619 errors:0 dropped:0 overruns:0 frame:0
TX packets:6619 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:912141 (912.1 KB) TX bytes:912141 (912.1 KB)
输出iwconfig
h2o@h2o-Vostro-1015:~$ iwconfig
lo no wireless extensions.
eth0 no wireless extensions.
eth1 IEEE 802.11 Access Point: Not-Associated
Link Quality:5 Signal level:211 Noise level:199
Rx invalid nwid:0 invalid crypt:0 invalid misc:0