我在Ubuntu中创建了一个服务器套接字,在Windows中创建了一个客户端套接字,但在服务器上拒绝连接。 它说:"连接被拒绝"。 我使用的是Ubuntu 16.04 LTS和Windows 10。 两个套接字都用C语言编写。 我使用" Linux GCC"在Eclipse中编译服务器套接字代码(在Ubuntu上运行)。 我使用" Cygwin GCC"在Eclipse中编译客户端套接字代码(在Windows上运行)。和Gnu Make Builder。 服务器的IP地址192.168.1.21 和客户的IP地址192.168.1.20
请检查以下代码段:
服务器套接字:
//to run this program in the terminal I enter one argument,
//the port to listen to, 34550.
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
void error(char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[])
{
char* register_emergency = "register emergency";
int sockfd, newsockfd, portno, clilen;
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
int n;
if (argc < 2) {
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]); //usually 34550
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 on binding");
listen(sockfd, 5);
clilen = sizeof(cli_addr);
while(1){
printf("now waiting for a new connection...\n"); //this appears on server's screen to me.
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
//on the "accept" function the control flow waits and holds for a connection
//As far as it goes for the server, it keeps waiting here and doesn't react... As if it doesn't see any incoming connection
if (newsockfd < 0) {
error("ERROR on accept");
} else {
bzero(buffer,256);
n = read( newsockfd, buffer, 255);
if (n < 0) error("ERROR reading from socket");
printf("Here is the message I got from Mr. Client: '%s'\n",buffer);
struct sockaddr_in *s = (struct sockaddr_in *) &cli_addr;
char ipstr[INET6_ADDRSTRLEN];
inet_ntop(AF_INET, &s->sin_addr, ipstr, sizeofipstr);
printf("The IP of the client: %s\n", ipstr);
printf("The port of the sender: %d\n",ntohs(s->sin_port));
if (strcmp( register_emergency, buffer) == 0){
}
n = write(newsockfd, "Hey Mr. Client, this is server talking...", 18);
if (n < 0) error("ERROR writing to socket");
}
}
}
现在,客户端在Windows上运行:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
void error(char *msg)
{
perror(msg);
exit(0);
}
//In the command prompt, I enter 2 arguments
//192.168.1.21 34550
int main(int argc, char *argv[])
{
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[256];
if (argc < 3) {
fprintf(stderr,"usage %s hostname port\n", argv[0]);
exit(0);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
serv_addr.sin_port = htons(portno);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr("192.168.1.21");
if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
error("ERROR connecting"); //program flow always enters here... then perror returns "Connection refused"
printf("Please enter the message: ");
bzero(buffer,256);
fgets(buffer,255,stdin);
n = write(sockfd,buffer,strlen(buffer));
if (n < 0)
error("ERROR writing to socket");
bzero(buffer,256);
n = read(sockfd,buffer,255);
if (n < 0)
error("ERROR reading from socket");
printf("%s\n",buffer);
return 0;
}
我尝试了另一个端口,但它没有用。 我试图在两台机器上反转客户端和服务器但是再次拒绝连接。
我检查了Wireshark中的数据包,从服务器生成的数据包始终是Seck = 1,Ack = 1,但是Rst = 1,所以它不是导致问题的路由器。 并且两台机器上的防火墙都已关闭。
答案 0 :(得分:2)
您忘记了客户端代码中的这一行
wifi = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);