user1721182
user1721182

Reputation: 171

Failing to Connect to Socket From Client Side

I'm trying to program a simple client and server using sockets in C. Both the client and server are running Ubuntu. The server is broadcasting an ad-hoc network that the however I cannot seem to get the client to connect to the server despite being able to ping it from terminal.

The code I'm using for the server and client are adapted from an online source and are as such:

#include<stdio.h>
#include<string.h>  //strlen
#include<sys/socket.h>
#include<arpa/inet.h>   //inet_addr
#include<unistd.h>  //write

int main(int argc , char *argv[])
{
int socket_desc , new_socket , c;
struct sockaddr_in server , client;
char *message;

//Create socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
    printf("Could not create socket");
}

//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 8888 );

//Bind
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{
    puts("bind failed");
    return 1;
}
puts("bind done");

//Listen
listen(socket_desc , 3);

//Accept and incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
new_socket = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c);
if (new_socket<0)
{
    perror("accept failed");
    return 1;
}

puts("Connection accepted");

//Reply to the client
message = "Hello Client , I have received your connection. But I have to go now, bye\n";
write(new_socket , message , strlen(message));

return 0;
}

Client

 #include <stdio.h>
 #include <string.h>    
 #include <sys/socket.h>
 #include <arpa/inet.h> 

int main(int argc , char *argv[])
{
int socket_desc;
struct sockaddr_in server;
char *message;

//Create socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
    printf("Could not create socket");
}

server.sin_addr.s_addr = inet_addr("192.168.0.1");
server.sin_family = AF_INET;
server.sin_port = htons( 80 );

//Connect to remote server
if (connect(socket_desc , (struct sockaddr *)&server , sizeof(server)) < 0)
{
    puts("connect error");
    return 1;
}

puts("Connected\n");

//Send some data
message = "GET / HTTP/1.1\r\n\r\n";
if( send(socket_desc , message , strlen(message) , 0) < 0)
{
    puts("Send failed");
    return 1;
}
puts("Data Send\n");

return 0;
}

The IP address was obtained by running ifconfig on the terminal of the server and looking at its inet addr value.

Additionally I've also disabled the firewall on both the client and the server by running sudo ufw disable in the terminal. Errno also outputs connection refused.

Can anyone please help me with this?

Upvotes: 0

Views: 3506

Answers (2)

Vishi
Vishi

Reputation: 11

Instead of directly using inet_addr("192.168.0.1");

use

struct sockaddr_in6 ser_address;
inet_pton(AF_INET,"192.168.0.1",&ser_address.sin.addr);

Note: We cannot use presentation style address in the codes when passing it to socket functions

Upvotes: 1

KBart
KBart

Reputation: 1608

Your server listens to port 8888 and client tries to connect to port 80, both server.sin_port = htons( x ); must use the same port number.

Upvotes: 3

Related Questions