LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices

Reply
 
Search this Thread
Old 10-29-2013, 01:17 AM   #1
why_so_serious
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
Code:
#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, "192.168.0.1",&(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;
		}
	else
		printf("Connected\n");
	/*printf("Please enter the message: ");
	bzero(buffer, 256);
	fgets(buffer,255,stdin);
	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);
	close(sockfd);*/
}
my server C# program in window is
Code:
namespace server_window
{
    public partial class Form1 : Form
    {
          static TcpListener tcpListener = new TcpListener(IPAddress.Parse("192.168.0.2"), 1234);
        TcpClient socketForClient;
       EndPoint ep;
       public Form1()
        {
            InitializeComponent();
            BW_Connection.RunWorkerAsync();
        }
      
        private void BW_Connection_DoWork(object sender, DoWorkEventArgs e)
        {
            tcpListener.Start();
            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)
        {
            socketForClient.Close();
            
        }

    }
}

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, 05:51 AM   #2
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 2,140

Rep: Reputation: 549Reputation: 549Reputation: 549Reputation: 549Reputation: 549Reputation: 549
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, 06:04 AM   #3
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,645

Rep: Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250
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?
 
  


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


All times are GMT -5. The time now is 11:38 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration