C# / JAVA Socket Programming

6 篇文章 0 订阅

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;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值