Wednesday, October 30, 2013

C# server in window and C client in linux

I wrote C# server in window and C client in Linux.


my c# server is



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 iin 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;
}
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

No comments:

Post a Comment