2011-04-06 73 views
0

我有一個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(); 
} 
} 
+1

看起來不錯。你的問題在哪裏? – 2011-04-06 13:38:33

+0

我必須問你在這裏有什麼期望有人爲你移植代碼? – Peter 2011-04-06 14:04:35

+0

歡迎來到StackOverflow。請查看[faq](http://stackoverflow.com/faq)。我還建議你閱讀[這篇博客文章](http://blog.stackoverflow.com/2010/10/asking-better-questions/)提示如何提出更好的問題。 – Will 2011-04-06 14:58:27

回答

1

一種策略可以工作:

  1. 在C#IDE的代碼粘貼。
  2. 正確的語法錯誤
  3. 查找類似未知類/方法XYZ錯誤
  4. 查找到API的Javadoc文檔C#的等效,並尋找應該做的一樣的Java C#類/方法 - 類,並相應地編輯你的代碼。
  5. 測試它。
+0

抱歉,我正在嘗試建議,最終確實有幫助..謝謝 – kay 2011-04-07 15:12:28