Can't make an applet to connect to socket server.

Hello,
I’m trying to get a simple server (application) / client (applet) to work. My problem is that it all works in eclipse… but when I open it in firefox I get a black rectangle… I made a jar package and signed it … ( I get the alert that my app’s digital signature cannot be verified and Do I want to run it ). ???

Could you post some code? :slight_smile:

SERVER:


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

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

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

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

        PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
        BufferedReader in = new BufferedReader(
				new InputStreamReader(
				clientSocket.getInputStream()));
        String inputLine, outputLine;
        KnockKnockProtocol kkp = new KnockKnockProtocol();

        outputLine = kkp.processInput(null);
        out.println(outputLine);

        while ((inputLine = in.readLine()) != null) {
             outputLine = kkp.processInput(inputLine);
             out.println(outputLine);
             if (outputLine.equals("Bye."))
                break;
        }
        out.close();
        in.close();
        clientSocket.close();
        serverSocket.close();
    }
}

APPLET:

import java.net.*;
import java.applet.*;
import java.awt.TextArea;
import java.io.*;


public class test extends Applet
{
	   TextArea answers = new TextArea(
       "Ready\n");

    	Socket socket = null;
    	

    	public void init(){
    	 answers.setEditable(false);
	      add(answers);

    	
       		try
		{
                 	socket = new Socket("xxx.xxx.xxx.xxx",4444);
              	}
                catch (UnknownHostException ex)
                {
                 System.out.println("Unknown host");
                 System.exit(1);
             	}
   	        catch (IOException exx)
            	{
   	        		System.out.println("Read Failed");
   	        		answers.append("READ FAILED");
                  	System.exit(1);
            	} 
	     String fromServer;
	     BufferedReader in = null;
	     try {
	     in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
	     }
	     catch (IOException ex)
	     {
	      answers.append("Couldn't get I/O for the connection.");
	     }
	  try { fromServer =in.readLine();
            answers.append("Server: " + fromServer);
            if (fromServer.equals("Bye.")) answers.append("END");
	  }
	  catch (IOException ex)
	  { answers.append("END OF TEXT");}
                
	     
	     
	     
    	}		
    	
}

I tested it under Firefox on linux and I had no problem (Although I did remove your the use of your KnockKnockProtocol and simply sent a test string). Perhaps it is a firewall issue? Also make sure you restart your server for the browser test as currently it will only serve the first connection. This could cause headaches if you test in Eclipse followed by Firefox. I would recommend printing to standard out and then checking the java console under Firefox…

Ammm when I run the server on ubuntu 10.10 I see the server runing on tcp v6
netstat -tapn | grep 4444:

tcp6       0      0 :::4444                 :::*                    LISTEN      28087/java

maybe this is a problem :?
Hmm if I run it on the same machine it works… however on a windows machine in work and on my netbook (all firefox) it does not start. No firewalls on both PCs

Maybe you have a router inbetween If you have windows then that one also has a built in firewall.

Kind regards,
Mike