基于networkcomms V3通信框架的c#服务器与java客户端进行通信探讨 – 风清气正

首先说一下networkcomms 通信框架是商业版的,本文也并不提供。

对java不太熟悉,刚刚安装了eclipse,通信只实现了把字符从java客户端传到c#服务器,服务器收到消息后,返回一个字符消息给java客户端,java客户端显示收到的消息。

服务器端基于networkcomms V3 C#通信框架.

服务器端代码:

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 NetworkCommsDotNet;
using NetworkCommsDotNet.DPSBase;
using NetworkCommsDotNet.Tools;

using NetworkCommsDotNet.Connections;
using NetworkCommsDotNet.Connections.TCP;
using System.Net;
using MessageContract;

namespace Server
{
public partial class Form1 : Form
{
SendReceiveOptions sro
= new SendReceiveOptions(DPSManager.GetDataSerializer<NullSerializer>(), null, null);

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
//服务器开始监听客户端的请求
//开始监听某端口

//与其他语言通信 禁用ApplicationLayerProtocolStatus
TCPConnectionListener listener=new TCPConnectionListener (sro,ApplicationLayerProtocolStatus.Disabled);

Connection.StartListening(listener, new IPEndPoint(IPAddress.Parse(txtIP.Text), int.Parse(txtPort.Text)));

button1.Text = 监听中;
button1.Enabled
= false;

//此方法中包含服务器具体的处理方法。
StartListening();
}

private void StartListening()
{
NetworkComms.DisableLogging();

//array 收到字节
NetworkComms.AppendGlobalIncomingUnmanagedPacketHandler((header, connection, array) =>
{
// C#语言UTF-8字节数组转换成字符串
string recvStr = Encoding.UTF8.GetString(array, 0, array.Length);

//记录一下 证明消息已经收到
string newStr = 收到的字符串是 + recvStr;

LogInfo.LogMessage(newStr, 日志.txt);

//返回消息给java客户端
string serverStr = 服务器发出的消息123;

byte[] newByte = Encoding.UTF8.GetBytes(serverStr);
//发送字节数据
connection.SendUnmanagedBytes(newByte);
});

}

private void Form1_FormClosing_1(object sender, FormClosingEventArgs e)
{
NetworkComms.Shutdown();
Environment.Exit(Environment.ExitCode);
}
}
}

客户端界面:

客户端java代码(对java不是很熟悉,代码可能有不妥之处):

package com.yee4.liu;
import java.io.BufferedReader;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;

public class helloworld extends Thread{

public static byte[] readStream(InputStream inStream) throws Exception {
int count = 0;
while (count == 0) {
count
= inStream.available();
}
byte[] b = new byte[count];
inStream.read(b);
return b;
}

/**
* @param args
*/
public static void main(String[] args) throws Exception {
Socket socket
= null;

int j = 0;
try {

socket = new Socket(127.0.0.1, 3000);
if (socket.isConnected()) {
System.
out.println(已建立连接!—);

String strContent=java发送的消息;

byte[] bs = strContent.getBytes(UTF-8);

//2.得到socket读写流
OutputStream os=socket.getOutputStream();

//3.利用流按照一定的操作,对socket进行读写操作

os.write(bs);
os.flush();

System.out.println(———————————-);

InputStream ips = socket.getInputStream();
byte[] rebyte = readStream(ips);

String resultStr = new String(rebyte, 0, rebyte.length, UTF-8);
System.
out.println(收到主机消息: + resultStr);

}

}

catch(Exception s){
System.
out.print(s);
}

}

}

回顾一下流程

(1):打开 C#程序,开始监听

 (2):运行java客户端,发送一个消息给c#服务器

   

String strContent=java发送的消息;

byte[] bs = strContent.getBytes(UTF-8);

//2.得到socket读写流
OutputStream os=socket.getOutputStream();

//3.利用流按照一定的操作,对socket进行读写操作

os.write(bs);
os.flush();

(3):c#服务器收到消息后,进行解析 

 <1>:

// C#语言UTF-8字节数组转换成字符串
string recvStr = Encoding.UTF8.GetString(array, 0, array.Length);

//记录一下 证明消息已经收到
string newStr = 收到的字符串是 + recvStr;

LogInfo.LogMessage(newStr, 日志.txt);

<2>返回消息给java客户端:

//返回消息给java客户端
string serverStr = 服务器发出的消息123;

byte[] newByte = Encoding.UTF8.GetBytes(serverStr);
//发送字节数据
connection.SendUnmanagedBytes(newByte);

(4):java客户端接收消息,并显示出来

InputStream ips = socket.getInputStream();
byte[] rebyte = readStream(ips);

String resultStr = new String(rebyte, 0, rebyte.length, UTF-8);
System.
out.println(收到主机消息: + resultStr);

java程序好像接收完消息后,就退出了,这里不知道怎样处理。

程序只在我的电脑测试成功,win7系统,未在其他系统进行测试。

c#工程文件下载 (不含通信框架)

java文件

参考文章:

http://www.cnblogs.com/iyangyuan/archive/2012/12/23/2829712.html

http://cuisuqiang.iteye.com/blog/1434416

http://blog.csdn.net/mecho/article/details/7342654

http://www.cnblogs.com/iyangyuan/archive/2012/12/23/2829712.html#3140522

http://cuisuqiang.iteye.com/blog/1434416

http://blog.csdn.net/paincupid/article/details/10635323

本文链接:基于networkcomms V3通信框架的c#服务器与java客户端进行通信探讨,转载请注明。



You must enable javascript to see captcha here!

Copyright © All Rights Reserved · Green Hope Theme by Sivan & schiy · Proudly powered by WordPress

无觅相关文章插件,快速提升流量