Java Applet 未加载 - 客户端/服务器

发布于 2024-09-11 21:07:54 字数 4285 浏览 1 评论 0原文

我有这个服务器和一个小程序。服务器加载良好,因为它可以检测新用户何时加载客户端并连接。问题是小程序无法加载,就像卡在加载屏幕上一样。

如果您愿意,这里是我的问题的视频解释:http://vimeo.com/13692709 (如果您想看到问题发生,建议观看视频...)

Server.java

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

public class Server{

    static  Socket clientSocket = null;
    static  ServerSocket serverSocket = null;

    static  clientThread t[] = new clientThread[10];


    public static void main(String args[]) {

    // The default port    

    int port_number=2222;

    if (args.length < 1)
        {
        System.out.println("Starting game server...\nPort number: "+port_number);
        } else {
        port_number=Integer.valueOf(args[0]).intValue();
        }

        try {
        serverSocket = new ServerSocket(port_number);
        System.out.println("\n**********************\n*** SERVER STARTED ***\n**********************\n");
        }
        catch (IOException e)
        {System.out.println(e);}

    while(true){
        try {
        clientSocket = serverSocket.accept();
        System.out.println("*** SOMEONE CONNECTED ***\n");
        for(int i=0; i<=9; i++){
            if(t[i]==null)
            {
                (t[i] = new clientThread(clientSocket,t)).start();
                break;
            }
        }
        }
        catch (IOException e) {
        System.out.println(e);}
    }
    }
}

class clientThread extends Thread{

    BufferedReader is = null;
    PrintStream os = null;
    Socket clientSocket = null;
    clientThread t[];
    Random roll = new Random();

    public clientThread(Socket clientSocket, clientThread[] t){
    this.clientSocket=clientSocket;
        this.t=t;
    }

    public void run()
    {
        System.out.println("Check 1");
    String line;
        String name;
        int py;
    try{
        System.out.println("Check 2");
        is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        System.out.println("Check 3");
        os = new PrintStream(clientSocket.getOutputStream());
        System.out.println("Check 4");
        int px = roll.nextInt(200);
        //os.println("Enter your name.");
        System.out.println("Check 5");
        name = "Guest #" + roll.nextInt(6);
        System.out.println("Check 6\n");
        System.out.println(name+" has entered the game. Their attack: " + px);
        System.out.println("Check 7");
        for(int i=0; i<=9; i++)
        if (t[i]!=null && t[i]!=this)
            t[i].os.println("*** A new user "+name+" entered the chat room !!! ***" );
            System.out.println("Check 8");
        while (true) {
            System.out.println("Check 9");
        line = is.readLine();
                if(line.startsWith("/quit")) break;
        for(int i=0; i<=9; i++)
            if (t[i]!=null)  t[i].os.println("<"+name+":"+px+"> "+line); // someone said something
        }
        for(int i=0; i<=9; i++)
        if (t[i]!=null && t[i]!=this) {
        System.out.println("\n" + name + " has left!\n");
            t[i].os.println("*** The user "+name+" is leaving the chat room !!! ***" );
        }

        os.println("*** Bye "+name+" ***");

        for(int i=0; i<=9; i++) {
        if (t[i]==this) t[i]=null;
        System.out.println("\nSomeone has left!\n");
    }

        is.close();
        os.close();
        clientSocket.close();
    }
    catch(IOException e){};
    }
}

ClientApplet.java

import java.io.*;
import java.applet.Applet;
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.util.*;

public class ClientApplet extends Applet {

    public static TextArea chat;

public void init() {
        chat = new TextArea(10,40);
            add(chat);
    chat.append("Hey!");


Socket s = null;

try {
s = new Socket(getParameter("host"), Integer.valueOf(getParameter("port")));
//s = new Socket("localhost", 4444);
InputStream in = s.getInputStream();
int buf = -1;
while ((buf = in.read()) != '.') {
System.out.print((char)buf);
}
}catch(Exception e) {
e.printStackTrace();
}
finally {
try {
    s.close();
    } catch(IOException e)
    { }
}

}
}

I have this server and an applet. The server loads fine because it can detect when a new user loads up the client and connects. The problem is that the Applet will not load, like it is STUCK at the loading screen.

Here is a video explanation of my problem if you'd like: http://vimeo.com/13692709
(watch the video is recommended if you want to SEE the problem happen... )

Server.java

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

public class Server{

    static  Socket clientSocket = null;
    static  ServerSocket serverSocket = null;

    static  clientThread t[] = new clientThread[10];


    public static void main(String args[]) {

    // The default port    

    int port_number=2222;

    if (args.length < 1)
        {
        System.out.println("Starting game server...\nPort number: "+port_number);
        } else {
        port_number=Integer.valueOf(args[0]).intValue();
        }

        try {
        serverSocket = new ServerSocket(port_number);
        System.out.println("\n**********************\n*** SERVER STARTED ***\n**********************\n");
        }
        catch (IOException e)
        {System.out.println(e);}

    while(true){
        try {
        clientSocket = serverSocket.accept();
        System.out.println("*** SOMEONE CONNECTED ***\n");
        for(int i=0; i<=9; i++){
            if(t[i]==null)
            {
                (t[i] = new clientThread(clientSocket,t)).start();
                break;
            }
        }
        }
        catch (IOException e) {
        System.out.println(e);}
    }
    }
}

class clientThread extends Thread{

    BufferedReader is = null;
    PrintStream os = null;
    Socket clientSocket = null;
    clientThread t[];
    Random roll = new Random();

    public clientThread(Socket clientSocket, clientThread[] t){
    this.clientSocket=clientSocket;
        this.t=t;
    }

    public void run()
    {
        System.out.println("Check 1");
    String line;
        String name;
        int py;
    try{
        System.out.println("Check 2");
        is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        System.out.println("Check 3");
        os = new PrintStream(clientSocket.getOutputStream());
        System.out.println("Check 4");
        int px = roll.nextInt(200);
        //os.println("Enter your name.");
        System.out.println("Check 5");
        name = "Guest #" + roll.nextInt(6);
        System.out.println("Check 6\n");
        System.out.println(name+" has entered the game. Their attack: " + px);
        System.out.println("Check 7");
        for(int i=0; i<=9; i++)
        if (t[i]!=null && t[i]!=this)
            t[i].os.println("*** A new user "+name+" entered the chat room !!! ***" );
            System.out.println("Check 8");
        while (true) {
            System.out.println("Check 9");
        line = is.readLine();
                if(line.startsWith("/quit")) break;
        for(int i=0; i<=9; i++)
            if (t[i]!=null)  t[i].os.println("<"+name+":"+px+"> "+line); // someone said something
        }
        for(int i=0; i<=9; i++)
        if (t[i]!=null && t[i]!=this) {
        System.out.println("\n" + name + " has left!\n");
            t[i].os.println("*** The user "+name+" is leaving the chat room !!! ***" );
        }

        os.println("*** Bye "+name+" ***");

        for(int i=0; i<=9; i++) {
        if (t[i]==this) t[i]=null;
        System.out.println("\nSomeone has left!\n");
    }

        is.close();
        os.close();
        clientSocket.close();
    }
    catch(IOException e){};
    }
}

ClientApplet.java

import java.io.*;
import java.applet.Applet;
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.util.*;

public class ClientApplet extends Applet {

    public static TextArea chat;

public void init() {
        chat = new TextArea(10,40);
            add(chat);
    chat.append("Hey!");


Socket s = null;

try {
s = new Socket(getParameter("host"), Integer.valueOf(getParameter("port")));
//s = new Socket("localhost", 4444);
InputStream in = s.getInputStream();
int buf = -1;
while ((buf = in.read()) != '.') {
System.out.print((char)buf);
}
}catch(Exception e) {
e.printStackTrace();
}
finally {
try {
    s.close();
    } catch(IOException e)
    { }
}

}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酸甜透明夹心 2024-09-18 21:07:54

我是怎么解决的?

通过在单独的线程中处理连接

How did I fix it?

By handling the connection in a separate thread

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文