[英]Port does not seem to be open, even though listening
My server is not going passed the listening stage, even though my client is trying to run the socket.connect()
function in a loop until connection is made.我的服务器没有通过监听阶段,即使我的客户端试图在一个循环中运行
socket.connect()
function 直到建立连接。 Is something flawed in my server code?我的服务器代码有问题吗?
#define PORT 8080
void server()
{
int serverSocket, newSocket;
struct sockaddr_in serverAddr;
struct sockaddr_storage serverStorage;
socklen_t addr_size;
int opt = TRUE;
//Create socket
serverSocket = socket(PF_INET, SOCK_STREAM, 0);
if (setsockopt(serverSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&opt,
sizeof(opt))<0)
error("SETSOCKOPT ERROR");
//Configure setting of the server address struct
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
serverAddr.sin_port = htons(PORT);
//Set all bits of the padding field to 0
memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);
//Bind the address struct to the socket
bind(serverSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr));
//Listen on the socket, with max 40 connection requests queued
if(listen(serverSocket,40)==0)
printf("\nListening\n");
else
printf("\nError\n");
pthread_t tid[60];
int i = 0;
while(1)
{
//Accept call creates a new socket for the incoming connection
addr_size = sizeof serverStorage;
printf("HERE");
newSocket = accept(serverSocket, (struct sockaddr *) &serverStorage, &addr_size);
//pthread_mutex_lock(&lock);
//for each client request creates a thread and assign the client request to it to process
//so the main thread can entertain next request
int error_thread = pthread_create(&tid[i], NULL, socketThread, (void *)(uintptr_t) newSocket);
if( error_thread != 0 )
{
printf("\nFailed to create thread, %i\n", error_thread);
rebootOWASYS();
}
else
{
printf("\nSOCKET %i CREATED\n", newSocket);
}
if( i >= 50)
{
i = 0;
while(i < 50)
{
pthread_join(tid[i++],NULL);
}
i = 0;
}
}
}
The server manages to print "Listening", however, my client is unable to connect (does not reach the "here" print).服务器设法打印“Listening”,但是,我的客户端无法连接(未到达“here”打印)。 Trying the
nc -zv 10.130.28.130 8080
is not successful, however pinging the server is.尝试
nc -zv 10.130.28.130 8080
不成功,但是对服务器执行 ping 操作。 My client is looping the socket.connect(10.130.28.130, 8080)
as long as no connections is accepted.只要不接受连接,我的客户端就会循环
socket.connect(10.130.28.130, 8080)
。
def try_connect(address):
global connected
global client
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.settimeout(2)
try:
client.connect(address)
connected = True
except:
client.close()
connected = False
finally:
return [client, connected]
Am I missing something obvious?我错过了一些明显的东西吗?
The first problem I see is trying to call the connect function without two sets of parentheses:我看到的第一个问题是尝试在没有两组括号的情况下调用连接 function:
client.connect((addr, port)) # Like that
Maybe you were overthinking it or I'm underthinking it.也许你想多了,或者我想多了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.