我怀疑这有一个简单的解决方案,我可以忽略,可能与客户有关或如何设置。
无论如何,我试图建立一个简单的Echo服务器/客户端来理解套接字编程的基础知识。我有一台运行Linux Mint的虚拟机,主机正在运行Windows 10.我设置的虚拟机运行服务器c代码,Windows将运行客户端。
我开始制作服务器代码
//Echo Server for UNIX: Using socket programming in C, a client sends a string
//to this server, and the server responds with the same string sent back to the client
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
int main()
{
char stringBuffer[50]; //string buffer for reading incoming and resending
int listener, communicator, c; //store values returned by socket system call
if((listener = socket(AF_INET, SOCK_STREAM, 0)) == -1) //creates a new socket
puts("Could not create socket");
puts("Socket Created");
struct sockaddr_in servAddr, client; //structure from <netinet/in.h> for address of server
servAddr.sin_family = AF_INET; //addressing scheme set to IP
servAddr.sin_port = htons(8888); //server listens to port 5000
servAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); //symbolic constant of server IP address
//binds the socket to the address of the current host and port# the server will run on
if (bind(listener, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0){
puts("Bind failed");
return 1;
}
puts("Bind Successful");
listen(listener, 5); //listens for up to 5 connections at a time
c = sizeof(struct sockaddr_in);
if ((communicator = accept(listener, (struct sockaddr*)&client, (socklen_t*)&c ))<0)
puts("accept failed");
puts("Connection Accepted");
//wait until someone wants to connect, then whatever is sent can be read from communicator, which can then be sent back
while(1){
bzero(stringBuffer, 50); //sets buffer to 0
read(communicator, stringBuffer, 50); //reads from communicator into buffer
write(communicator, stringBuffer, strlen(stringBuffer)+1); //returns back
}
return 0;
}
之后我通过打开来宾机中的另一个终端测试了它并输入&#34; telnet localhost 8888&#34;并输入我想要的任何字符串。
现在,这个测试工作在我的Windows机器上,以创建套接字编程的客户端:
#include <winsock.h>
#include <stdio.h>
#include <string.h>
#pragma comment(lib,"ws2_32.lib") //Winsock Library
int main(int argc, char *argv[])
{
WSADATA wsadata; //variable for using sockets in windows
SOCKET sock; //socket variable for network commands
char sendString[50], recieveString[50]; //variables for sending and recieving messages to/from server
//check if WSA initialises correctly
if (WSAStartup(MAKEWORD(2,2), &wsadata) != 0)
printf("Error Code: %d", WSAGetLastError());
//creates new socket and saves into sock
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
printf("Could not create socket: %d", WSAGetLastError());
printf("Socket created\n");
struct sockaddr_in servAddr;
servAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); //sets the IP address to the same machine as the server
servAddr.sin_family = AF_INET; //addressing scheme set to TCP/IP
servAddr.sin_port = htons(8888); //server address is on port 8888
//connects to device with specifications from servAddr
if (connect(sock, (struct sockaddr *)&servAddr, sizeof(servAddr)) < 0) {
printf("Connection Error %d\n", WSAGetLastError());
return 1;
}
printf("Connection Accepted\n");
while(1){
fgets(sendString, 50, stdin); //uses stdin to get input to put into sendString
//sends sendString to server using sock's properties
if (send(sock, sendString, strlen(sendString) + 1, 0) < 0); {
printf("Send Failed");
return 0;
}
//reads from server into recieveString
if ((recv(sock, recieveString, 50, 0)) == SOCKET_ERROR)
printf("Recieve Failed");
printf("%s", recieveString); //prints out recieveString
}
}
现在,在服务器仍然运行的情况下,当我尝试客户端时,我得到响应&#34;连接错误&#34; (来自第35行)。看了Unix和WinSock的例子后,我不确定为什么我会失败连接。我怀疑它可能与Linux VM的窗口有关,但我不确定。
--- --- UPDATE 更新了意外分号并添加了WSAGetLastError后,它显示的错误代码为10061;这转化为 &#34;连接被拒绝。 无法建立连接,因为目标计算机主动拒绝它。这通常是因为尝试连接到在外部主机上处于非活动状态的服务,即没有运行服务器应用程序的服务。&#34;
答案 0 :(得分:3)
if (connect(sock, (struct sockaddr *)&servAddr, sizeof(servAddr)) < 0); {
printf("Connection Error");
return 1;
}
这只是一个微不足道的语法错误。您无条件进入该区块。删除第一个分号。
然而,有一个更重要的要点。当您从系统调用(例如connect()
)收到错误时,您必须打印错误。不只是你自己设计的一些信息。否则,你不知道你是否只是一个错误,一个临时问题,一个持久的问题,或一个永久的问题。
将printf()
更改为:
printf("Connect error %s\n", WSAGetLastError());
然后不要继续,好像错误没有发生。
请注意,这适用于所有系统调用,具体包括socket()
,bind()
,listen()
,connect()
,accept()
,recv()
,send()
和朋友。
答案 1 :(得分:3)
[第3次编辑后:]
Sry,重新阅读你的问题。重要的是:
我设置的虚拟机运行服务器c代码,Windows将运行客户端。
127.0.0.1是始终仅本地到启用IP的框的地址。因此,您的服务器正在侦听Linux VM本地接口127.0.0.1,并且客户端尝试连接到Windows框的本地127.0.0.0。这两个接口不相同。结果很明显,即客户端找不到任何要连接的东西。
127.0.0.1(所谓的&#34; IPv4本地环回接口 e&#34;)只能用于连接 local 到一个框。