Java and C# Client Server Socket Programming


C# client namespaces
----------------
1.System
2.System.Net.Sockets
3.System.IO;

java packages
--------------
1.java.net
2.java.io

Java server Application

import java.net.*;
import java.io.*;
public class java_server
{
public static void main(String h[])
{
try
{
ServerSocket ss=new ServerSocket(1800);
Socket s=ss.accept();
System.out.println("Client Accepted");
BufferedReader br=new BufferedReader(new
InputStreamReader(s.getInputStream()));
System.out.println(br.readLine());
PrintWriter wr=new PrintWriter(new
OutputStreamWriter(s.getOutputStream()),true);
wr.println("Welcome to Socket Programming");
}catch(Exception e){System.out.println(e);}
}
}

Compile using
javac java_server.java

C# client Application


using System;
using System.Net.Sockets;
using System.IO;
class csharp_client
{
public static void Main(string[] args)
{
try{
TcpClient tc=new TcpClient("server",1800);// in the place of server, enter
your java server's hostname or Ip
Console.WriteLine("Server invoked");
NetworkStream ns=tc.GetStream();
StreamWriter sw=new StreamWriter(ns);
sw.WriteLine("My name is Pramod.A");
sw.Flush();
StreamReader sr=new StreamReader(ns);
Console.WriteLine(sr.ReadLine());
}catch(Exception e){Console.WriteLine(e);}
}

}

Compile using the command
csc /r:System.dll csharp_client.cs

After compilation, run the java server first
java java_server

Then run the c# client application
csharp_client

Related posts:

  1. Sockets Programming in Java
  2. Howto PHP / Java bridge on Debian
  3. Ubuntu Howto: Install Sun Java
  4. Dalvik: How Google routed around Sun’s IP-based licensing restrictions on Java ME
  5. Java default keystore password – cacerts
  6. Debugging Server-side Code through IntelliJ IDEA or Eclipse with BEA Weblogic 8.1
  7. Generic SOAP/WSDL Client
  8. Howto install Sun Java on Debian Sarge
  9. Howto Install Sun Java on Debian Etch
  10. Java and C benchmark

Popular Related Items »

3 Comments »

  1. Aya said,

    May 8, 2009 @ 10:51

    Please , help me to write a program between server and client in two states : in same computer , with two different computers . . . . Thanks

  2. Shaykh said,

    June 10, 2009 @ 12:17

    Article is good. However it looks like some kind of seo site. The main purpose is not solving some exact problem. BUt upgradeing the rating of site . It is seen like abc ..

  3. Frank said,

    October 17, 2009 @ 16:05

    Hi,

    at first, sorry for my english … i hope you will understand my problem
    My java serversocket runs in a while(true) loop and listen at accept() the incomming .net clients. Each accepted client socket start a new thread. If only one client connect to the java socket server, all works nice, but if I use more than one client parallel, the second client will be blocked and I get the esception: A blocking operation was interrupted by a call to WSACancelBlockingCall
    I thougt the problem is, that both clients (each came from an other pc with it’s own IP) like to comunicate over the same server socket port. Is this the reason? And how I can solve this.

    Thanks for your help.

RSS feed for comments on this post · TrackBack URI

Leave a Comment