LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to connect c# server in window and c Clinet in Linux (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-connect-c-server-in-window-and-c-clinet-in-linux-4175482438/)

why_so_serious 10-28-2013 03:26 AM

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

pan64 10-28-2013 05:51 AM

I think you need to use the correct host names.

jpollard 10-28-2013 07:01 AM

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.

clintwelbar 11-04-2013 01:47 AM

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

why_so_serious 11-04-2013 01:54 AM

Quote:

Originally Posted by clintwelbar (Post 5057979)
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. :(


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