So I've been grinding over the summer to make sure I'm ahead of my class when the school year starts, and I recently started dabbing into sockets and datagram servers etc. I haven't fully wrapped my head around it so logic would dictate that I would eventually run into a problem... Here I am...
Ok so I have two classes here; My client singleton that connects to the port and host name I specified and my server singelton that binds to a port. Here they are:
SERVERSINGLETON:
package org.com.eng.server; import javax.swing.*; import java.net.*; import java.io.*; public class ServerSingleton implements Runnable { public ServerSingleton(int port)throws IOException{ try { } catch(Exception e){ System.out.println("Could not listen on the specified port. Might be in use..."); } try { ServerSocket ServSocket = new ServerSocket(port); Socket ClntSocket; ClntSocket = ServSocket.accept(); PrintWriter out = new PrintWriter(ClntSocket.getOutputStream(), true); BufferedReader in = new BufferedReader(new InputStreamReader(ClntSocket.getInputStream())); String userIn = in.readLine(); if(userIn != null){ System.out.println("Recieved Data!"); } if(userIn.equalsIgnoreCase("wai")){ out.println("You are Patrick."); System.out.println("Recieved Data!"); } while(userIn != null){ System.out.println("Recieved Data!"); } } catch (SocketException e){ e.printStackTrace(); System.out.println("Something goofed when determining the server socket"); System.exit(-1); } try { } catch (Exception e) { e.printStackTrace(); } } public void run(){ try { getServerSingleton(); } catch (Exception e){ } } /* * @author Patrick Worlds * @return Returns the current ServerSingleton */ public static synchronized ServerSingleton getServerSingleton() throws IOException{ if(Server == null) Server = new ServerSingleton(4445); return Server; } public Object clone() throws CloneNotSupportedException { JOptionPane.showMessageDialog(null, "Bad boy! >:( Go sit in corner nao!"); throw new CloneNotSupportedException(); } private static ServerSingleton Server; }
CLIENTSINGLETON:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; import javax.swing.JOptionPane; public class ClientSingleton implements Runnable { private Socket ClntSocket; public ClientSingleton(int port, String hostname)throws IOException{ try { ClntSocket = new Socket(hostname, port); BufferedReader stdIn = new BufferedReader( new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(ClntSocket.getOutputStream(), true); BufferedReader in = new BufferedReader(new InputStreamReader(ClntSocket.getInputStream())); if(ClntSocket.isConnected() == true){ System.out.println("Connection established"); } String userIn; String serverOut = in.readLine(); userIn = stdIn.readLine(); while(userIn != null){ out.println(userIn); System.out.println("You: " +userIn); } while(serverOut != null){ System.out.println("Server: " +serverOut); } } catch (Exception e){ e.printStackTrace(); System.out.println("Something goofed when determining a connection..."); System.exit(-1); } } public void run(){ try { getClientSingleton(); } catch (Exception e){ } } /* * @author Patrick Worlds * @return Returns the current ClientSingleton */ public static synchronized ClientSingleton getClientSingleton() throws IOException{ if(Client == null) Client = new ClientSingleton(4445, "127.0.0.1"); return Client; } public Object clone() throws CloneNotSupportedException { JOptionPane.showMessageDialog(null, "1 at a time ;)"); throw new CloneNotSupportedException(); } private static ClientSingleton Client; }
Now a problem arose. That being that every time I attempt to input something (namely "wai") it seems that either the server ignores it, or never even receives it. I'd really love if someone could help me out here. >.<
-Thanks