Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 10-29-2013, 02:17 AM   #1
LQ Newbie
Registered: Oct 2013
Posts: 26

Rep: Reputation: Disabled
socket programming cannot connect

Hi everyone, I am writing client C socket program in Linux and server C# socket program in window. I am also running these 2 programs in different PCs. When I run client program it shows that "Cannot connect".
my client C program in Linux is
#include <sys/socket.h>  //for socket(), connect(), sendto() and recvform()
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>  // for printf() and fprintf()
#include <string.h>  //for memset()
#include <stdlib.h>   //for atoi() and exit()
#include <unistd.h>  //for close()
#include <errno.h>
#include <arpa/inet.h>  //for sockaddr_in and inet_addr()

#define SERVERPORT 1234	

int main ( )                      
	int sockfd, n;
	struct sockaddr_in serv_addr;
	struct hostent *serverINFO;
	char buffer[256];
	char hostname[1024];
	//struct hostent* h;
	gethostname(hostname, sizeof(hostname));
	serverINFO = gethostbyname(hostname);
	if(serverINFO == NULL)
		printf("Problem interpreting host\n");
		return 1;  
	sockfd = socket(AF_INET, SOCK_STREAM, 0);
	if (sockfd < 0)
			printf("Cannot create socket\n");
			return 1;
	inet_pton(AF_INET, "",&(serv_addr.sin_addr));
	//serv_addr.sin_family = serverINFO ->h_addrtype;
	//memcpy((char *) &serv_addr.sin_addr, serverINFO->h_addr_list[0], serverINFO->h_length);
	//serv_addr.sinport = htons(SERVERPORT);
	//bzero((char *) &serv_addr, sizeof(serv_addr));
	//serv_addr.sin_family = AF_INET;
	//memcpy(&serv_addr.sin_addr, server->h_addr, server->h_length);
	//bcopy((char *)server->h_addr,(char *)&serv_addr.sin_addr.s_addr,server->h_length);
	serv_addr.sin_port = htons (SERVERPORT);
	if(connect(sockfd, (struct sockaddr *)&serv_addr, sizeof (serv_addr)) < 0) 
			printf("Cannot connect\n");
			return 1;
	/*printf("Please enter the message: ");
	bzero(buffer, 256);
	n = write(sockfd, buffer, strlen(buffer));
	if (n < 0)
		printf ("ERROR writing to socket");
	bzero(buffer, 256);
	n = read (sockfd, buffer, 255);
	if (n < 0)   
		printf ("ERROR reading from socket");
	printf("here we are %s\n", buffer);
my server C# program in window is
namespace server_window
    public partial class Form1 : Form
          static TcpListener tcpListener = new TcpListener(IPAddress.Parse(""), 1234);
        TcpClient socketForClient;
       EndPoint ep;
       public Form1()
        private void BW_Connection_DoWork(object sender, DoWorkEventArgs e)
            socketForClient = tcpListener.AcceptTcpClient();
            ep = socketForClient.Client.RemoteEndPoint;
              this.Invoke(new MethodInvoker(delegate { TB_text.Text = "Client connected..."; }));
           private void Form1_FormClosing(object sender, FormClosingEventArgs e)


If my program is wrong, pls kindly let me know. Coz I am a bit new to socket programming and Linux. Thanks a lot..
Old 10-29-2013, 06:51 AM   #2
Senior Member
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 3,314

Rep: Reputation: 818Reputation: 818Reputation: 818Reputation: 818Reputation: 818Reputation: 818Reputation: 818
I think you already asked this and got the answer.

You can't use the local host name as if it is a remote system - unless you only want to connect to local services.
Old 10-29-2013, 07:04 AM   #3
LQ Guru
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 6,735

Rep: Reputation: 1808Reputation: 1808Reputation: 1808Reputation: 1808Reputation: 1808Reputation: 1808Reputation: 1808Reputation: 1808Reputation: 1808Reputation: 1808Reputation: 1808
Why do you repeat your threads? If you need explanation or further help you can just use your original thread(s) and type your question. Marking the old one solved (and saying thanks) means you are satisfied with that solution.

Just ask your question and also try to understand the responses. If those answers were not really suitable for you please state it and ask further (without opening new threads).

Where is this code coming from?


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] How to connect a java client to a BSD server using socket programming sanju.lnt Programming 2 08-06-2011 03:57 PM
Can't connect to UNIX socket /var/run/clamav/clamd.socket ganick Linux - Server 8 08-01-2008 02:22 PM
Unable to connect to UNIX socket /tmp/.esd/socket error while using grip dr_zayus69 Linux - Software 4 08-23-2005 08:28 PM

All times are GMT -5. The time now is 12:31 AM.

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