'What is causing connection error 111 refused to connect
I am trying to have python connect to a tcp server written in C, the server and the client python script are running on the same machine (Linux). The C code compiles and runs without error. Yes they are using the same port of 27001. There can be no use of 3rd party libraries. Python says that the issue is on client.connect((HOST,PORT)), I get no read out from the C side of things. I've tried changing the ports, I know that these ports are unused as they are ports for many steam games, none of which are running while the server is online. I ran sudo netstat -ntlp in the terminal and I do not see my server, nor do I see the process id, or the port that I bound the server too.
Python:
import socket
def clientTest():
HOST=socket.gethostbyname("localhost")
print(HOST)
PORT=27001
client=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((HOST,PORT))
for i in range(5):
toSend="Data "
toSend+=str(i)
client.send(toSend)
print("Sent data")
time.sleep(2)
resp=client.recv(1024)
print(resp)
clientTest()
C:
char* serverListen(server *s, int *bytesWritten){
int listenRet=listen(s->socketFd, maxConnections);
if(listenRet==0){
unsigned int clientLen;
s->incomingSocket=accept(s->socketFd, (struct sockaddr*)&s->clientIn, &clientLen);
if(s->incomingSocket<0){
printf("Server Listen problem\n\tAccept issue: %i\n", s->incomingSocket);
close(s->incomingSocket);
return NULL;
}
char *buffer=(char*)calloc(s->bufferSize, sizeof(char));
int bytesRead=read(s->incomingSocket, buffer, s->bufferSize-1);
if(bytesRead<0){
printf("Server Listen Problem\n\tRead issue: %i\n", bytesRead);
free(buffer);
close(s->incomingSocket);
return NULL;
}
#ifdef DEBUG
printf("Bytes read: %i\nMessage:\t%s\n", bytesRead, buffer);
#endif
int writeRet=write(s->incomingSocket, "Message recived", 16);
if(writeRet<0){
printf("Server Listen Problem\n\tWrite issue: %i\n", writeRet);
close(s->incomingSocket);
return NULL;
}
*bytesWritten=bytesRead;
close(s->incomingSocket);
return buffer;
}
return NULL;
}
void* singleThreadServerFunc(void *params){
server *s=(server*)params;
char *retData;
int bytesWritten;
while(s->serverShouldRun){
retData=serverListen(s, &bytesWritten);
if(bytesWritten>0){
printf("%s", retData);
}
free(retData);
}
return NULL;
}
server *initServer(int port){
server *out=(server*)calloc(1, sizeof(struct server));
out->socketFd=socket(AF_INET, SOCK_STREAM, 0);
if(out->socketFd<0){
printf("Server init problem\n\tsockFd is less than 0!\n");
freeServer(out);
return NULL;
}
int opts=1;
int sockOption=setsockopt(out->socketFd, SOL_SOCKET, SO_REUSEADDR, &opts, sizeof(opts));
if(sockOption<0){
printf("Server init problem\n\tSocket Option issue\n");
freeServer(out);
return NULL;
}
out->portNumber=port;
out->serverIn.sin_family=AF_INET;
out->serverIn.sin_addr.s_addr=INADDR_ANY;
out->serverIn.sin_port=htons(out->portNumber);
memset(&out->serverIn.sin_zero, 0, sizeof(char));
int bindRet=bind(out->socketFd, (struct sockaddr*)&out->serverIn, sizeof(out->serverIn));
if(bindRet<0){
printf("Server init problem\n\tBind issue %i\n", bindRet);
freeServer(out);
return NULL;
}
out->bufferSize=1024;
pthread_create(&out->serverThread, NULL, singleThreadServerFunc, out);
return out;
}
int main(){
server *s=initServer(DefaultPort);
if(s!=NULL){
while(1){
char c[2];
scanf("%1s",c);
if(c[0]=='q')
break;
}
freeServer(s);
}
return 0;
}
Solution 1:[1]
I found the answer, it was in singleThreadServerFunc I did not have s->serverShouldRun set to true
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | BackSpace7777777 |
