When multiple object streams are used, input streams have to be initialized in the same order as output streams because ObjectOutputStream
sends a header to the other party and ObjectInputStream
blocks execution until it reads the header.
import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; public class NetworkAppExample { public static void main(String[] args) throws Exception { String host = "localhost"; int port = 10430; ServerSocket server = new ServerSocket(port, 50, InetAddress.getByName(host)); System.out.println("Server started."); Socket client = new Socket(host, port); System.out.println("Connecting to server..."); Socket connection = server.accept(); System.out.println("Connection established."); ObjectOutputStream clientOut = new ObjectOutputStream(client.getOutputStream()); ObjectOutputStream serverOut = new ObjectOutputStream(connection.getOutputStream()); ObjectInputStream clientIn = new ObjectInputStream(client.getInputStream()); ObjectInputStream serverIn = new ObjectInputStream(connection.getInputStream()); } }
Order as specified in the code above might be easier to remember - first initialize output streams then input streams in the same order. However, another order for initialization of object streams is the following:
ObjectOutputStream clientOut = new ObjectOutputStream(client.getOutputStream()); ObjectInputStream serverIn = new ObjectInputStream(connection.getInputStream()); ObjectOutputStream serverOut = new ObjectOutputStream(connection.getOutputStream()); ObjectInputStream clientIn = new ObjectInputStream(client.getInputStream());
// code omitted import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; public class NetworkAppExample { public static void main(String[] args) throws Exception { String host = "localhost"; int port = 10430; ServerSocket server = new ServerSocket(port, 50, InetAddress.getByName(host)); System.out.println("Server started."); Socket client = new Socket(host, port); System.out.println("Connecting to server..."); Socket connection = server.accept(); System.out.println("Connection established."); // code omitted System.out.println("Communication is ready."); } }
Hello World
text will be sent to the server either as byte[]
or String
. Declare a variable of the type that depends on the stream used. Use byte[]
for data streams and String
for object streams. String
. In this case, String
is converted to byte[]
instead of written using writeBytes()
method to show how it would be done with other objects, such as images or other files. import java.io.DataInputStream; import java.io.DataOutputStream; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; public class NetworkAppExample { public static void main(String[] args) throws Exception { String host = "localhost"; int port = 10430; ServerSocket server = new ServerSocket(port, 50, InetAddress.getByName(host)); System.out.println("Server started."); Socket client = new Socket(host, port); System.out.println("Connecting to server..."); Socket connection = server.accept(); System.out.println("Connection established."); DataOutputStream clientOut = new DataOutputStream(client.getOutputStream()); DataInputStream clientIn = new DataInputStream(client.getInputStream()); DataOutputStream serverOut = new DataOutputStream(connection.getOutputStream()); DataInputStream serverIn = new DataInputStream(connection.getInputStream()); System.out.println("Communication is ready."); byte[] messageOut = "Hello World".getBytes(); } }
import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; public class NetworkAppExample { public static void main(String[] args) throws Exception { String host = "localhost"; int port = 10430; ServerSocket server = new ServerSocket(port, 50, InetAddress.getByName(host)); System.out.println("Server started."); Socket client = new Socket(host, port); System.out.println("Connecting to server..."); Socket connection = server.accept(); System.out.println("Connection established."); ObjectOutputStream clientOut = new ObjectOutputStream(client.getOutputStream()); ObjectOutputStream serverOut = new ObjectOutputStream(connection.getOutputStream()); ObjectInputStream clientIn = new ObjectInputStream(client.getInputStream()); ObjectInputStream serverIn = new ObjectInputStream(connection.getInputStream()); System.out.println("Communication is ready."); String messageOut = "Hello World"; } }
import java.io.DataInputStream; import java.io.DataOutputStream; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; public class NetworkAppExample { public static void main(String[] args) throws Exception { String host = "localhost"; int port = 10430; ServerSocket server = new ServerSocket(port, 50, InetAddress.getByName(host)); System.out.println("Server started."); Socket client = new Socket(host, port); System.out.println("Connecting to server..."); Socket connection = server.accept(); System.out.println("Connection established."); DataOutputStream clientOut = new DataOutputStream(client.getOutputStream()); DataInputStream clientIn = new DataInputStream(client.getInputStream()); DataOutputStream serverOut = new DataOutputStream(connection.getOutputStream()); DataInputStream serverIn = new DataInputStream(connection.getInputStream()); System.out.println("Communication is ready."); byte[] messageOut = "Hello World".getBytes(); clientOut.writeInt(messageOut.length); clientOut.write(messageOut); clientOut.flush(); } }
import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; public class NetworkAppExample { public static void main(String[] args) throws Exception { String host = "localhost"; int port = 10430; ServerSocket server = new ServerSocket(port, 50, InetAddress.getByName(host)); System.out.println("Server started."); Socket client = new Socket(host, port); System.out.println("Connecting to server..."); Socket connection = server.accept(); System.out.println("Connection established."); ObjectOutputStream clientOut = new ObjectOutputStream(client.getOutputStream()); ObjectOutputStream serverOut = new ObjectOutputStream(connection.getOutputStream()); ObjectInputStream clientIn = new ObjectInputStream(client.getInputStream()); ObjectInputStream serverIn = new ObjectInputStream(connection.getInputStream()); System.out.println("Communication is ready."); String messageOut = "Hello World"; clientOut.writeObject(messageOut); clientOut.flush(); } }
import java.io.DataInputStream; import java.io.DataOutputStream; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; public class NetworkAppExample { public static void main(String[] args) throws Exception { String host = "localhost"; int port = 10430; ServerSocket server = new ServerSocket(port, 50, InetAddress.getByName(host)); System.out.println("Server started."); Socket client = new Socket(host, port); System.out.println("Connecting to server..."); Socket connection = server.accept(); System.out.println("Connection established."); DataOutputStream clientOut = new DataOutputStream(client.getOutputStream()); DataInputStream clientIn = new DataInputStream(client.getInputStream()); DataOutputStream serverOut = new DataOutputStream(connection.getOutputStream()); DataInputStream serverIn = new DataInputStream(connection.getInputStream()); System.out.println("Communication is ready."); byte[] messageOut = "Hello World".getBytes(); clientOut.writeInt(messageOut.length); clientOut.write(messageOut); clientOut.flush(); System.out.println("Message sent to server: " + new String(messageOut)); } }
import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; public class NetworkAppExample { public static void main(String[] args) throws Exception { String host = "localhost"; int port = 10430; ServerSocket server = new ServerSocket(port, 50, InetAddress.getByName(host)); System.out.println("Server started."); Socket client = new Socket(host, port); System.out.println("Connecting to server..."); Socket connection = server.accept(); System.out.println("Connection established."); ObjectOutputStream clientOut = new ObjectOutputStream(client.getOutputStream()); ObjectOutputStream serverOut5 ★ | 1 Vote