C#和Java套接字如何通信 [英] C# and Java Sockets how to comunicate

查看:89
本文介绍了C#和Java套接字如何通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我通过以下方式用套接字实现了C#服务器:
我有一个听众:

Hello,

I have C# server implemented with sockets in this way:
I have a listener:

TcpListener listener = new TcpListener(IPAddress.Any, 33333);
listener.Start();



每当有客户端连接时,我都会创建一个新客户端



I created a new client every time a client connects

TcpClient client = listener.AcceptTcpClient();



还有BinaryReader和BinaryWriter:



And a BinaryReader and BinaryWriter:

NetworkStream stream = client.GetStream();
w = new BinaryWriter(stream);
r = new BinaryReader(stream);



如果我想向连接的客户端写或读东西,我正在使用:



If I want to write or read something to connected client i am using:

w.Write(MESSAGE);//for writing
msg=r.ReadString();//for reading something that client sends



问题:
我如何创建一个与该服务器通信的Java程序,我只能设法连接到服务器(创建套接字),但是我在Google上找不到有关如何发送和接收消息的任何内容,我尝试了不同的方法,但是没有写或读任何东西.有什么建议?或一个非常简单的Java程序就足够了.谢谢

问候,
Sas Gabriel



The problem:
How can I create a java program to comunicate with this server, I only managed to connect to the server(create the socket) but i didn''t find anything on google about how to send and receive messages, I tried different ways but it didn''t write or read anything. Any suggestions? or a very simple java program would be just enough. Thanks

Regards,
Sas Gabriel

推荐答案

使用Google查找Java中套接字的源代码.一旦创建了套接字,该语言就与它无关.唯一的问题是,您在客户端和服务器之间传输的数据必须采用双方都同意并理解的形式.简单的ASCII字节可能是最好的形式,可能有一个标头字节给出了消息的长度.
Use Google to find source code for sockets in Java. Once you have created the socket the language has nothing to do with it. The only issue is that the data you transfer between client and server must be in a form that both ends agree and understand. Simple ASCII bytes is probably the best form, with maybe a header byte giving the length of the message.


进一步了解Richard的出色答案,请看 ^ ]中的一些简单示例来编写Java TCP/IP客户端.
Further to Richard''s excellent answer, look here[^] for some easy examples of writing a Java TCP/IP client.


这篇关于C#和Java套接字如何通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆