Hello HF, I'm currently learning Socket Programming in C.
I was wondering why my Client program can connect to my Server program, but when I try to use NetCat to act as the listening server, the Client can't connect.
Client.c
Server.c
When I try to connect my Client to NetCat it returns this:
I even decided to copy someone else's code on YouTube. Since he was doing it on Windows, I hopped onto my Windows machine and did the same thing he did. He even used NetCat as a listener. Unfortunately I'm still not able to connect to Netcat. I've been trying to find answers for this all day yesterday but with no luck. I was considering asking Stack Overflow, but lately I've been getting nothing but sarcastic remarks or no answers... Any help and explanation would be VERY much appreciated. Thank you for your time :)
I was wondering why my Client program can connect to my Server program, but when I try to use NetCat to act as the listening server, the Client can't connect.
Client.c
Code
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main()
{
int netSocket;
netSocket = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in serverAddr;
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(9001);
serverAddr.sin_addr.s_addr = INADDR_ANY;
int hasConnected = connect(netSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr));
if(hasConnected == -1) {
printf("There was an error making a connection to the remote socket.\n\n");
}
char serverRes[500];
recv(netSocket, &serverRes, sizeof(serverRes), 0);
printf("The server sent the data: %s\n", serverRes);
close(netSocket);
return 0;
}Server.c
Code
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
int main()
{
char serverMsg[500] = "You have reached the server!";
int serverSocket;
serverSocket = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in serverAddr;
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(9001);
serverAddr.sin_addr.s_addr = INADDR_ANY;
bind(serverSocket, (struct sockaddr*) &serverAddr, sizeof(serverAddr));
listen(serverSocket, 5);
int clientSocket;
clientSocket = accept(serverSocket, NULL, NULL);
send(clientSocket, serverMsg, sizeof(serverMsg), 0);
close(serverSocket);
return 0;
}When I try to connect my Client to NetCat it returns this:
Code
There was an error making a connection to the remote socket.
The server sent the data: �I even decided to copy someone else's code on YouTube. Since he was doing it on Windows, I hopped onto my Windows machine and did the same thing he did. He even used NetCat as a listener. Unfortunately I'm still not able to connect to Netcat. I've been trying to find answers for this all day yesterday but with no luck. I was considering asking Stack Overflow, but lately I've been getting nothing but sarcastic remarks or no answers... Any help and explanation would be VERY much appreciated. Thank you for your time :)

