gpt4 book ai didi

c# - Java 服务器,C# 客户端。发送数据

转载 作者:行者123 更新时间:2023-12-01 13:18:53 24 4
gpt4 key购买 nike

所以,我在将数据从 C# 客户端发送到 Java 服务器时遇到问题。连接正在进行中,但我猜我在某个地方弄乱了一些东西。

这是服务器端代码

package com.chris.net;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;


public class Server implements Runnable
{
private String serverName;
private boolean isRunning;

private ServerSocket serverSocket;
private Socket clientSocket;

public Server(String name, int port)
{
try
{
this.serverName = name;
this.serverSocket = new ServerSocket(port);
this.isRunning = true;
new Thread(this).start();
}
catch (IOException e)
{
e.printStackTrace();
}
}

private BufferedReader recv;

public void run()
{
while(isRunning)
{
try
{
clientSocket = serverSocket.accept();
System.out.println("Client Connected from " + clientSocket.getInetAddress().getHostAddress() + ":" + clientSocket.getPort());

recv = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

System.out.println("Data Recieved: " + recv.readLine());

clientSocket.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}

这是客户端代码

lass TCPClient
{
private TcpClient Client;
private NetworkStream Stream;

private Byte[] Data;

public TCPClient(string address, int port)
{
Client = new TcpClient();
Client.Connect(address, port);

Stream = Client.GetStream();

SendData("Test Data");

while (Client.Connected)
{

}
}

public void SendData(string message)
{
Data = System.Text.Encoding.ASCII.GetBytes(message);
Stream.Write(Data, 0, Data.Length);

Console.WriteLine("Sent: {0}", message);
}
}

服务器注册连接,客户端似乎认为它已经发送了数据,但是我无法判断是客户端没有发送数据还是服务器没有接收到数据。考虑到 Console.Writeline 只是打印出转换为字节的消息,我无法判断。

最佳答案

如果您想使用 JSON,请参阅 this关于让 Java 发送将 java 类型序列化为 JSON 的消息以及接收 C# 以 Deserealize 为 C# 类型的优秀文章。

Java 摘录:

import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import com.google.gson.Gson;

public class GuyServer {
public static void main(String[] args)
throws IOException {
Gson gson = new Gson();
ServerSocket serverSocket = new ServerSocket(16001);
System.out.println("Listening on port 16001. "
+ "Press enter to quit "
+ "after the next connection.");
while (System.in.available() == 0) {
Socket socket = serverSocket.accept();
System.out.println("A client has connected."
+ " Sending a new Guy down the pipe.");
PrintWriter out =
new PrintWriter(socket.getOutputStream(),
true);
Guy guy = Guy.getRandomGuy();
String json = gson.toJson(guy);
out.print(json);
out.close();
socket.close();
}
System.out.println("Quitting...");
serverSocket.close();
}
}

C# 结束:

using System;
using System.IO;
using System.Net.Sockets;
using System.Web.Script.Serialization;

class GuyClient
{
static void Main(string[] args)
{
String input;

using (TcpClient tcpClient =
new TcpClient("localhost", 16001))
using (NetworkStream networkStream =
tcpClient.GetStream())
using (StreamReader streamReader =
new StreamReader(networkStream))
{
input = streamReader.ReadToEnd();
}

Console.WriteLine("Received data: " + input + "\n");

JavaScriptSerializer javaScriptSerializer =
new JavaScriptSerializer();
Guy bob = javaScriptSerializer
.Deserialize<Guy>(input) as Guy;
Console.WriteLine(bob.ToString());
}
}

Java 类

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Guy {
private String name;
public String getName() {
return name;
}
}

}

C# 可串行化类

using System;
using System.Collections.Generic;

[Serializable]
class Guy
{
public string Name { get; set; }
}

我对此进行了一些清理,以使其更容易理解。它不会编译,但你明白了。

关于c# - Java 服务器,C# 客户端。发送数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22214926/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com