LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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-28-2013, 03:26 AM   #1
why_so_serious
LQ Newbie
 
Registered: Oct 2013
Posts: 26

Rep: Reputation: Disabled
how to connect c# server in window and c Clinet in Linux


Hi everyone, at first, I am just a newbie for Linux and c#. How can I connect C# server socket in window to C client socket in Linux? Please kindly guide me.
I wrote C# server in window and C client in Linux.

my c# server is
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using System.Drawing.Imaging;


namespace server_window
{
    public partial class Form1 : Form
    {
        Socket m_mainSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        IPEndPoint iplocal = new IPEndPoint(IPAddress.Any, 1234);
        Socket client;
        IPEndPoint newclient;
        public Form1()
        {
            InitializeComponent();
            BW_Connection.RunWorkerAsync();
        }
      
        private void BW_Connection_DoWork(object sender, DoWorkEventArgs e)
        {
            m_mainSocket.Bind(iplocal);
            m_mainSocket.Listen(4);
            client = m_mainSocket.Accept();
            newclient = (IPEndPoint)client.RemoteEndPoint;
            this.Invoke(new MethodInvoker(delegate { TB_text.Text = "Client connected..."; }));
        }
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            client.Close();
            
        }
    }
}
my C client 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;
		}
	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);
	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);
}
I run each program in one PC. I used cross cable to connect both pc. But when i run both program, the client terminal showed that "cannot connect". .. how can i get the connection between these two? if my code is wrong, pls kindly guide me. thanks a lot
 
Old 10-28-2013, 05:51 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321
I think you need to use the correct host names.
 
Old 10-28-2013, 07:01 AM   #3
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Your use of "gethostname(hostname, sizeof(hostname));" limits you to a poor assumption:

That the local host name is the name of your server... AND even that the local host name even has an IP address assigned.

The hostname/IP number of the server MUST be a parameter to the application (the usual case), or it can be read in from elsewhere.
 
Old 11-04-2013, 01:47 AM   #4
clintwelbar
LQ Newbie
 
Registered: Nov 2013
Posts: 2

Rep: Reputation: Disabled
C# socket programming

Here is a basic level c# socket program (not in lynux) C# Socket Programming . From this program you can understand how to connect server and client. Hope it will help you.

tks/Clint
 
Old 11-04-2013, 01:54 AM   #5
why_so_serious
LQ Newbie
 
Registered: Oct 2013
Posts: 26

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by clintwelbar View Post
Here is a basic level c# socket program (not in lynux) C# Socket Programming . From this program you can understand how to connect server and client. Hope it will help you.

tks/Clint
hi, thanks for your answer. I wrote C# client and server socket program. It is ok. And also tried with C client and server program in Linux. Also ok. The problem is when I wrote C client program in Linux and C# server program in window. Between them I connect with crossover cable. But when I run my programs, my c client program gives error like Connection refused. So that's y.
 
  


Reply



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
Linux server and clinet sunhui Linux - Distributions 2 01-18-2007 03:15 AM
Can't connect to X11 window server kofibull Linux - Newbie 10 06-16-2006 11:47 PM
Connect to window print server yhus Linux - Networking 1 07-12-2005 07:07 AM
Can't connect to X11 window server suresheva31 Linux - General 0 10-19-2004 02:17 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:12 PM.

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
Open Source Consulting | Domain Registration