By Pramod AchuthanKutty
This example demonstrates how a c# client application can establish itself in a socket connection with a java server application through TCP/IP connectivity.
The example involves the attributes:
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 >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 The java server receives the following message >Client Accepted >My name is Pramod.A The c# client receives the following message >Server Invoked >Welcome to Socket Programming
This example demonstrates how a c# client can communicate with a java server or vice versa through its own TCP/IP implemented socket classes;