Hi
I have the following code which runs successfully as a stand alone example
when I try to incorporate it into my java program I get an error message
Error message (on Server): Cannot make a static reference to the non-static field server
Please advise what I need to change in the code to get it to work ?
Bob M
New Zealand
SocketServer console: Waiting for the client request Message Received: GBPAUDtest-data Waiting for the client request SocketClient console: Sending request to Socket Server Message: GBPAUDtest-data SocketServer: import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.net.ServerSocket; import java.net.Socket; /** * This class implements java Socket server * */ public class SocketServer { // static ServerSocket variable private static ServerSocket server; // socket server port on which it will listen private static int port = 9876; public static void main(String args[]) throws IOException, ClassNotFoundException { // create the socket server object server = new ServerSocket(port); // keep listens indefinitely until receives 'exit' call or program // terminates while (true) { System.out.println("Waiting for the client request"); // creating socket and waiting for client connection Socket socket = server.accept(); // read from socket to ObjectInputStream object ObjectInputStream ois = new ObjectInputStream( socket.getInputStream()); // convert ObjectInputStream object to String String message = (String) ois.readObject(); System.out.println("Message Received: " + message); // create ObjectOutputStream object ObjectOutputStream oos = new ObjectOutputStream( socket.getOutputStream()); // write object to Socket oos.writeObject(message); // close resources ois.close(); oos.close(); socket.close(); // terminate the server if client sends exit request if (message.equalsIgnoreCase("exit")) break; } System.out.println("Shutting down Socket server!!"); // close the ServerSocket object server.close(); } } // end of SocketServer SocketClient import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; /** * This class implements java socket client * */ public class SocketClient { public static void main(String[] args) throws UnknownHostException, IOException, ClassNotFoundException, InterruptedException { String Trading_Decision_2 = "test-data"; // get Computer 1 (server) IP address InetAddress host = (InetAddress.getByName("127.0.0.2")); Socket socket = null; ObjectOutputStream oos = null; ObjectInputStream ois = null; // establish socket connection to server socket = new Socket(host.getHostName(), 9876); // write to socket using ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream()); // Socket System.out.println("Sending request to Socket Server"); oos.writeObject("GBPAUD" + Trading_Decision_2); // read the server response message ois = new ObjectInputStream(socket.getInputStream()); String message = (String) ois.readObject(); System.out.println("Message: " + message); // close resources ois.close(); oos.close(); Thread.sleep(100); } } // end of SocketClient