SimpleSocketServer.java Source code

Java tutorial

Introduction

Here is the source code for SimpleSocketServer.java

Source

    import java.io.ObjectOutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;

public class SimpleSocketServer {

  public static void main(String args[]) throws Exception {
    ServerSocket serverSocket;
    int portNumber = 1777;
    Socket socket;
    String str;

    str = " <?xml version=\"1.0\" encoding=\"UTF-8\"?>";
    str += "<ticketRequest><customer custID=\"1\">";
    str += "</ticketRequest>";

    serverSocket = new ServerSocket(portNumber);

    System.out.println("Waiting for a connection on " + portNumber);

    socket = serverSocket.accept();

    ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());

    oos.writeObject(str);

    oos.close();

    socket.close();

  }

}

    import java.io.ObjectInputStream;
    import java.net.InetAddress;
    import java.net.Socket;

    public class SimpleSocketClient {

        public static void main(String args[]) throws Exception {
            Socket socket;
            int portNumber = 1777;
            String str = "";

            socket = new Socket(InetAddress.getLocalHost(), portNumber);

            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
            str = (String) ois.readObject();
            System.out.println(str);
        }

    }