如何解决网络Java代码中的“非法启动”错误?

时间:2015-05-04 01:52:12

标签: java network-programming

我想在Java中运行多线程Echo服务器/客户端对。

到目前为止,这是我的代码,对于客户端

import java.sql.Timestamp;
import java.util.Date;

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

public class EchoClient implements Runnable {

    private Socket server;
    private String line,input;

    String serverHostname = new String ("127.0.0.1");
   //echoSocket = new Socket(serverHostname, 10007);

    EchoClient(Socket server) {
      this.server=server;
    }



 public static void main(String[] args) throws IOException {


  public void run () {

      input="";

      try {
        // Get input from the client
        DataInputStream in = new DataInputStream (server.getInputStream());
        PrintStream out = new PrintStream(server.getOutputStream());

        while((line = in.readLine()) != null && !line.equals(".")) {
          input=input + line;
          out.println("I got:" + line);
        }

        // Now write to the client

        System.out.println("Overall message is:" + input);
        out.println("Overall message is:" + input);

        server.close();
      } catch (IOException ioe) {
        System.out.println("IOException on socket listen: " + ioe);
        ioe.printStackTrace();
      }
    }


}

}

我的服务器,在这里:

import java.text.SimpleDateFormat;
import java.sql.Timestamp;

import java.util.Date;
import java.util.Calendar;

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

public class EchoServer 
{ 
 public static void main(String[] args) throws IOException 
   { 
    ServerSocket serverSocket = null; 

    try { 
         serverSocket = new ServerSocket(10007); 
        } 
    catch (IOException e) 
        { 
         System.err.println("Could not listen on port: 10007."); 
         System.exit(1); 
        } 

        Socket clientSocket = null; 
        System.out.println ("Waiting for connection.....");

    try { 
         clientSocket = serverSocket.accept(); 
        } 
    catch (IOException e) 
        { 
         System.err.println("Accept failed."); 
         System.exit(1); 
        } 

    System.out.println ("Connection successful");
    System.out.println ("Waiting for input.....");




    //added in TimeStamp code
    java.util.Date date = new java.util.Date();
    String timeString = String.valueOf((date.getTime()));

    //String timeString = (String)(new Timestamp(date.getTime()));

    PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), 
                                      true); 
    BufferedReader in = new BufferedReader( 
            new InputStreamReader( clientSocket.getInputStream())); 

    String inputLine; 

    while ((inputLine = in.readLine()) != null) 
        { 
         System.out.println ("Server: " + inputLine); 
         out.println(inputLine + timeString ); 

         if (inputLine.equals("Bye.")) 
             break; 
        } 

    out.close(); 
    in.close(); 
    clientSocket.close(); 
    serverSocket.close(); 
   } 
} 

然而,当我运行我的代码时,我收到此错误:

  

EchoClient.java:24:错误:非法启动表达式public void   run(){^

谢谢

0 个答案:

没有答案