2009-11-02 2 views
0

TCPListener sample on MSDN를 살펴 보자 C#C#으로 변환하는 방법 (아래 코드 참조)?

import java.io.*; 
import java.net.*; 

class SimpleServer 
{ 
private static SimpleServer server; 
ServerSocket socket; 
Socket incoming; 
BufferedReader readerIn; 
PrintStream printOut; 

public static void main(String[] args) 
{ 
    int port = 8080; 

    try 
    { 
     port = Integer.parseInt(args[0]); 
    } 
    catch (ArrayIndexOutOfBoundsException e) 
    { 
     // Catch exception and keep going. 
    } 

    server = new SimpleServer(port); 
} 

private SimpleServer(int port) 
{ 
    System.out.println(">> Starting SimpleServer"); 
    try 
    { 
     socket = new ServerSocket(port); 
     incoming = socket.accept(); 
     readerIn = new BufferedReader(new InputStreamReader(incoming.getInputStream())); 
     printOut = new PrintStream(incoming.getOutputStream()); 
     printOut.println("Enter EXIT to exit.\r"); 
     out("Enter EXIT to exit.\r"); 
     boolean done = false; 
     while (!done) 
     { 
      String str = readerIn.readLine(); 
      if (str == null) 
      { 
       done = true; 
      } 
      else 
      { 
       out("Echo: " + str + "\r"); 
       if(str.trim().equals("EXIT")) 
       { 
        done = true; 
       } 
      } 
      incoming.close(); 
     } 
    } 
    catch (Exception e) 
    { 
     System.out.println(e); 
    } 
} 

private void out(String str) 
{ 
    printOut.println(str); 
    System.out.println(str); 
} 
} 

답변

3

뭔가 작업을해야합니다 :

using System; 
using System.IO; 
using System.Net.Sockets; 
using System.Text; 

namespace Server 
{ 
    internal class SimpleServer 
    { 
     private static SimpleServer server; 
     private readonly TcpListener socket; 

     private SimpleServer(int port) 
     { 
      Console.WriteLine(">> Starting SimpleServer"); 
      socket = new TcpListener(port); 
      socket.Start(); 
     } 

     private void DoJob() 
     { 
      try 
      { 
       bool done = false; 
       while (!done) 
       { 
        TcpClient client = socket.AcceptTcpClient(); 
        NetworkStream stream = client.GetStream(); 
        var reader = new StreamReader(stream, Encoding.UTF8); 
        String str = reader.ReadLine(); 
        if (str == null) 
        { 
         done = true; 
        } 
        else 
        { 
         printOut(str, stream); 
         if (str.Trim() == "EXIT") 
         { 
          done = true; 
         } 
        } 
        client.Close(); 
       } 
      } 
      catch (Exception e) 
      { 
       Console.WriteLine(e); 
      } 
     } 


     public static void main(String[] args) 
     { 
      int port = 8080; 

      try 
      { 
       port = Int32.Parse(args[0]); 
      } 
      catch (Exception e) 
      { 
       // Catch exception and keep going. 
      } 

      server = new SimpleServer(port); 
      server.DoJob(); 
     } 

     private void printOut(String str, Stream stream) 
     { 
      byte[] bytes = Encoding.UTF8.GetBytes("Echo: " + str + "\r\n"); 
      stream.Write(bytes, 0, bytes.Length); 
      Console.WriteLine(str); 
     } 
    } 
} 

편집 :을하지만 인코딩에 대해 경고 인코딩

+0

일에주의. –

1

이 번역하는 방법. 위에서 뭘 하려는지와 매우 흡사하며, 약간의 차이점을 포팅하는 것은 쉬워야합니다. 이 같은

관련 문제