将Java TCP套接字聊天程序转换为.net C#TCP聊天程序
问题描述:
我有一个Java TCP套接字聊天我想转换为.net C#程序。代码如下...请帮助。将Java TCP套接字聊天程序转换为.net C#TCP聊天程序
import java.io.*;
import java.net.*;
class Connection
{
public Socket s;
public PrintWriter o;
public BufferedReader i;
}
class TCPChatServerThreadTask extends Thread
{
Connection c;
public TCPChatServerThreadTask (ServerSocket serverSocket) throws IOException
{
c = new Connection();
c.s = serverSocket.accept();
c.o = new PrintWriter (c.s.getOutputStream(), true);
c.i = new BufferedReader (new InputStreamReader (c.s.getInputStream()));
System.out.println (c.s.getInetAddress() + ":" + c.s.getPort() + " Connected");
this.start();
}
public void run()
{
String fromClient = ">";
try
{
do
{
fromClient = c.i.readLine();
System.out.println (c.s.getInetAddress() + ":" + c.s.getPort() + "> " + fromClient);
for (int i = 0; i < TCPChatServerThread.taskCount - 1; i ++)
{
TCPChatServerThread.task[i].c.o.println (c.s.getInetAddress() + ":" + c.s.getPort() + "> " + fromClient);
}
}
while (fromClient != "quit");
System.out.println (c.s.getInetAddress() + ":" + c.s.getPort() + " Disconnected");
c.o.close();
c.i.close();
c.s.close();
}
catch (IOException e)
{
}
}
}
public class TCPChatServerThread
{
ServerSocket serverSocket = null;
static public String str = "?";
static public TCPChatServerThreadTask[] task = new TCPChatServerThreadTask[10];
static public int taskCount = 0;
public TCPChatServerThread() throws IOException
{
try
{
serverSocket = new ServerSocket (4455);
}
catch (IOException e)
{
System.err.println ("Server: Could not listen on port: ");
System.exit (1);
}
System.out.println ("Server: Listening on port: ");
while (true)
{
task[taskCount ++] = new TCPChatServerThreadTask (serverSocket);
}
}
public static void main (String[] args) throws IOException
{
TCPChatServerThread e = new TCPChatServerThread();
}
}
答
一种策略可以工作:
- 在C#IDE的代码粘贴。
- 正确的语法错误
- 查找类似未知类/方法XYZ错误
- 查找到API的Javadoc文档C#的等效,并寻找应该做的一样的Java C#类/方法 - 类,并相应地编辑你的代码。
- 测试它。
+0
抱歉,我正在尝试建议,最终确实有帮助..谢谢 – kay 2011-04-07 15:12:28
看起来不错。你的问题在哪里? – 2011-04-06 13:38:33
我必须问你在这里有什么期望有人为你移植代码? – Peter 2011-04-06 14:04:35
欢迎来到*。请查看[faq](http://*.com/faq)。我还建议你阅读[这篇博客文章](http://blog.*.com/2010/10/asking-better-questions/)提示如何提出更好的问题。 – Will 2011-04-06 14:58:27