Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

Socket Programming - How to Connect to NetCat

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
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 :)
What's the netcat command you are issuing?
(Jan 28, 2022 03:01 PM)yuumi Wrote:  What's the netcat command you are issuing?

The netcat command I'm using is:

Code
nc -l 9001

I'm sorry, I should've probably specified that sooner...
(Jan 28, 2022 03:05 PM)LilDeepWeb Wrote:  The netcat command I'm using is:

Code
nc -l 9001

I'm sorry, I should've probably specified that sooner...
The reason is becauase your client is waiting for netcat to send a message while netcat is doing the same.
This will work.
Code
int main()
{
int netSocket;
netSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

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 packet[] = "hello";
    send(netSocket, &packet, sizeof(packet), 0);

close(netSocket);

return 0;
}


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by mafiaboySep 6, 2019 11:37 AM 13 434 Sep 6, 2019 11:37 AM
Last Post: zp_city
[C] by Truth3356Jul 3, 2019 04:34 AM 12 389 Jul 3, 2019 04:34 AM
Last Post: m1n0r1tyrep0rt
[C] by Truth3356Jun 2, 2019 10:02 PM 6 175 Jun 2, 2019 10:02 PM
Last Post: Truth3356