LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 02-28-2013, 11:55 PM   #1
dnhan1
LQ Newbie
 
Registered: Feb 2013
Posts: 2

Rep: Reputation: Disabled
Client socket can't Connect to Server: Connection Refused


Hi 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:

Server
Code:
#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( 8889 );

//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
Code:
 #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( 8889 );

//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;
}
he 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. I've tried pinging the address which works, however trying to connect to port 8889 at the IP address using telnet does not work either


Can anyone please help me with this?

Last edited by dnhan1; 03-01-2013 at 12:33 AM.
 
Old 03-01-2013, 12:31 AM   #2
sevs
Member
 
Registered: Jul 2008
Location: Russia, Saratov
Distribution: debian, knoppix, mandriva, asplinux, altlinux
Posts: 110

Rep: Reputation: 20
Please, tell me you ain't running anything on your server at port 80?
At the server side:
Code:
//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 8889 );
At the client side:
Code:
server.sin_addr.s_addr = inet_addr("192.168.0.1");
server.sin_family = AF_INET;
server.sin_port = htons( 80 );
So, you try to connect to server on port 80 but your server is running on port 8889.
 
Old 03-01-2013, 12:34 AM   #3
dnhan1
LQ Newbie
 
Registered: Feb 2013
Posts: 2

Original Poster
Rep: Reputation: Disabled
Ah sorry! I actually copied an old version of the code, I actually fixed this error earlier when one of my acquaintances pointed it out to me. >.<
I've fixed it in the actual post now though
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Can't connect to UNIX socket /var/run/clamav/clamd.ctl: Connection refused williatf Linux - Server 15 09-08-2017 03:01 AM
unable to connect to socket connection refused 111: Tiger vnc Gil@LQ Linux - Virtualization and Cloud 9 03-30-2012 11:32 AM
connection refused in my client server program jamesbon Programming 1 10-23-2010 10:28 PM
error: unable to connect to socket: Connection refused (111) ; Mukiri Linux - Newbie 1 05-04-2009 03:27 AM
Client refused to connect to printer via print server kgao Linux - Networking 3 02-10-2005 08:31 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 04:56 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration