日期:2014-05-20  浏览次数:20758 次

急!大家看看!!!
// 服务器端 UploadServer.java
import java.net.*;
import java.io.*;
import java.util.*;

public class UploadServer {
private static Socket clientSocket;
private static int counter;

public static void main(String[] args) {
int i = 1;
ServerSocket serverSocket;
try {
serverSocket = new ServerSocket(8765);
while (true) {
  // 监听与本 socket 的连接并且接受它
clientSocket = serverSocket.accept();
// 创建线程,将上传数据写至服务器的文件中
Thread t = new MyThread(clientSocket, i);
t.start();
i++;
}
} catch (Exception e) {
System.out.println(e);
}
}
}
就是"Thread t = new MyThread(clientSocket, i);"提示有错误,但是我现在水平解决不了,希望大家在空闲的时候帮我看看,先谢谢了.


------解决方案--------------------
今天刚好看到Thread这里`呵呵~
------解决方案--------------------
你的MyThread类呢?

参考一下下面例子
Java code
import java.io.*;
import java.net.*;
import java.util.*;

public class ThreadedEchoServer {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {
            int i = 1;
            ServerSocket ss = new ServerSocket(8191);
            while (true) {
                Socket incoming = ss.accept();
                System.out.println("Spawning " + i);
                Runnable r = new ThreadedEchoHandler(incoming, i);
                Thread t = new Thread(r);
                t.start();
                i++;
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

class ThreadedEchoHandler implements Runnable {
    private Socket incoming;

    private int i;

    ThreadedEchoHandler(Socket incoming, int i) {
        this.incoming = incoming;
        this.i = i;
    }

    public void run() {
        // TODO Auto-generated method stub
        try {
            try {
                InputStream is = incoming.getInputStream();
                OutputStream os = incoming.getOutputStream();
                Scanner in = new Scanner(is);
                PrintWriter out = new PrintWriter(os, true);
                out.println("Hello! Enter BYE to exit!");
                boolean done = false;
                while (!done && in.hasNextLine()) {
                    String line = in.nextLine();
                    out.println("Echo: " + line);
                    if (line.trim().equals("BYE"))
                        done = true;
                }
            } finally {
                incoming.close();
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

------解决方案--------------------
看看你的MyThread类有没有类似MyThread(Socket s, int n); 这样的构造方法

------解决方案--------------------
又是线程..和加载的问题,.~~
------解决方案--------------------
没有那样的构造方法,那有什么样的构造方法?你按照那个构造方法new就行了

------解决方案--------------------
^_^ 路过 ^_^