Table of Contents
This updated guide examines Server: How to Create a Network Application in Java and organizes the essential facts, background, and practical takeaways in clear American English.
Steps
Create a class. Create a class and name it however you want. In this article, it will be namedNetworkAppExample.publicclassNetworkAppExample{}
Create a main method. Create a main method and declare it might throw exceptions ofExceptiontype and any subclass of it - all exceptions. This is considered a bad practice, but is acceptable for barebone examples.publicclassNetworkAppExample{publicstaticvoidmain(String[]args)throwsException{}}
Declare server address. This example will use local host address and an arbitrary port number. Port number needs to be in a range from 0 to 65535 (inclusive). However, port numbers to avoid range from 0 to 1023 (inclusive) because they are reserved system ports.
publicclassNetworkAppExample{publicstaticvoidmain(String[]args)throwsException{Stringhost="localhost";intport=10430;}}
Create a server. Server is bound to the address and port and listens for incoming connections. In Java,ServerSocketrepresents server-side endpoint and its function is accepting new connections.ServerSocketdoes not have streams for reading and sending data because it does not represent connection between a server and a client.importjava.net. InetAddress;importjava.net. ServerSocket;publicclassNetworkAppExample{publicstaticvoidmain(String[]args)throwsException{Stringhost="localhost";intport=10430;ServerSocketserver=newServerSocket(port,50,InetAddress.getByName(host));}}
Log server inception. For logging purposes, print to the console that server has been started.
importjava.net. InetAddress;importjava.net. ServerSocket;publicclassNetworkAppExample{publicstaticvoidmain(String[]args)throwsException{Stringhost="localhost";intport=10430;ServerSocketserver=newServerSocket(port,50,InetAddress.getByName(host));System.out.println("Server started.");}}
Create a client. Client is bound to the address and port of a server and listens for packets (messages) after connection is established. In Java,Socketrepresents either a client-side endpoint connected to the server or a connection (from server) to client and is used to communicate with the party on the other end.importjava.net. InetAddress;importjava.net. ServerSocket;importjava.net. Socket;publicclassNetworkAppExample{publicstaticvoidmain(String[]args)throwsException{Stringhost="localhost";intport=10430;ServerSocketserver=newServerSocket(port,50,InetAddress.getByName(host));System.out.println("Server started.");Socketclient=newSocket(host,port);}}
Log connection attempt. For logging purposes, print to the console that connection has been attempted.
importjava.net. InetAddress;importjava.net. ServerSocket;importjava.net. Socket;publicclassNetworkAppExample{publicstaticvoidmain(String[]args)throwsException{Stringhost="localhost";intport=10430;ServerSocketserver=newServerSocket(port,50,InetAddress.getByName(host));System.out.println("Server started.");Socketclient=newSocket(host,port);System.out.println("Connecting to server...");}}
Establish connection. Clients will never connect unless server listens for and accepts, in other words establishes, connections. In Java, connections are established usingaccept()method ofServerSocketclass. The method will block execution until a client connects.importjava.net. InetAddress;importjava.net. ServerSocket;importjava.net. Socket;publicclassNetworkAppExample{publicstaticvoidmain(String[]args)throwsException{Stringhost="localhost";intport=10430;ServerSocketserver=newServerSocket(port,50,InetAddress.getByName(host));System.out.println("Server started.");Socketclient=newSocket(host,port);System.out.println("Connecting to server...");Socketconnection=server.accept();}}
Log established connection. For logging purposes, print to the console that connection between server and client has been established.
importjava.net. InetAddress;importjava.net. ServerSocket;importjava.net. Socket;publicclassNetworkAppExample{publicstaticvoidmain(String[]args)throwsException{Stringhost="localhost";intport=10430;ServerSocketserver=newServerSocket(port,50,InetAddress.getByName(host));System.out.println("Server started.");Socketclient=newSocket(host,port);System.out.println("Connecting to server...");Socketconnection=server.accept();System.out.println("Connection established.");}}
Prepare communication streams. Communication is done over streams and, in this application, raw streams of (connection from) server (to client) and client need to be chained to either data or object streams. Remember, both parties need to use the same stream type.
- Data streams
importjava.io. DataInputStream;importjava.io. DataOutputStream;importjava.net. InetAddress;importjava.net. ServerSocket;importjava.net. Socket;publicclassNetworkAppExample{publicstaticvoidmain(String[]args)throwsException{Stringhost="localhost";intport=10430;ServerSocketserver=newServerSocket(port,50,InetAddress.getByName(host));System.out.println("Server started.");Socketclient=newSocket(host,port);System.out.println("Connecting to server...");Socketconnection=server.accept();System.out.println("Connection established.");DataOutputStreamclientOut=newDataOutputStream(client.getOutputStream());DataInputStreamclientIn=newDataInputStream(client.getInputStream());DataOutputStreamserverOut=newDataOutputStream(connection.getOutputStream());DataInputStreamserverIn=newDataInputStream(connection.getInputStream());}} - Object streams When multiple object streams are used, input streams have to be initialized in the same order as output streams because
ObjectOutputStreamsends a header to the other party andObjectInputStreamblocks execution until it reads the header.importjava.io. ObjectInputStream;importjava.io. ObjectOutputStream;importjava.net. InetAddress;importjava.net. ServerSocket;importjava.net. Socket;publicclassNetworkAppExample{publicstaticvoidmain(String[]args)throwsException{Stringhost="localhost";intport=10430;ServerSocketserver=newServerSocket(port,50,InetAddress.getByName(host));System.out.println("Server started.");Socketclient=newSocket(host,port);System.out.println("Connecting to server...");Socketconnection=server.accept();System.out.println("Connection established.");ObjectOutputStreamclientOut=newObjectOutputStream(client.getOutputStream());ObjectOutputStreamserverOut=newObjectOutputStream(connection.getOutputStream());ObjectInputStreamclientIn=newObjectInputStream(client.getInputStream());ObjectInputStreamserverIn=newObjectInputStream(connection.getInputStream());}}ObjectOutputStreamclientOut=newObjectOutputStream(client.getOutputStream());ObjectInputStreamserverIn=newObjectInputStream(connection.getInputStream());ObjectOutputStreamserverOut=newObjectOutputStream(connection.getOutputStream());ObjectInputStreamclientIn=newObjectInputStream(client.getInputStream());
- Data streams
Log that communication is ready. For logging purposes, print to the console that communication is ready.
// code omittedimportjava.net. InetAddress;importjava.net. ServerSocket;importjava.net. Socket;publicclassNetworkAppExample{publicstaticvoidmain(String[]args)throwsException{Stringhost="localhost";intport=10430;ServerSocketserver=newServerSocket(port,50,InetAddress.getByName(host));System.out.println("Server started.");Socketclient=newSocket(host,port);System.out.println("Connecting to server...");Socketconnection=server.accept();System.out.println("Connection established.");// code omittedSystem.out.println("Communication is ready.");}}
Create a message. In this application,Hello Worldtext will be sent to the server either asbyte[]orString. Declare a variable of the type that depends on the stream used. Usebyte[]for data streams andStringfor object streams.- Data streams Using data streams, serialization is done by converting objects into primitive data types or a
String. In this case,Stringis converted tobyte[]instead of written usingwriteBytes()method to show how it would be done with other objects, such as images or other files.importjava.io. DataInputStream;importjava.io. DataOutputStream;importjava.net. InetAddress;importjava.net. ServerSocket;importjava.net. Socket;publicclassNetworkAppExample{publicstaticvoidmain(String[]args)throwsException{Stringhost="localhost";intport=10430;ServerSocketserver=newServerSocket(port,50,InetAddress.getByName(host));System.out.println("Server started.");Socketclient=newSocket(host,port);System.out.println("Connecting to server...");Socketconnection=server.accept();System.out.println("Connection established.");DataOutputStreamclientOut=newDataOutputStream(client.getOutputStream());DataInputStreamclientIn=newDataInputStream(client.getInputStream());DataOutputStreamserverOut=newDataOutputStream(connection.getOutputStream());DataInputStreamserverIn=newDataInputStream(connection.getInputStream());System.out.println("Communication is ready.");byte[]messageOut="Hello World".getBytes();}} - Object streams
importjava.io. ObjectInputStream;importjava.io. ObjectOutputStream;importjava.net. InetAddress;importjava.net. ServerSocket;importjava.net. Socket;publicclassNetworkAppExample{publicstaticvoidmain(String[]args)throwsException{Stringhost="localhost";intport=10430;ServerSocketserver=newServerSocket(port,50,InetAddress.getByName(host));System.out.println("Server started.");Socketclient=newSocket(host,port);System.out.println("Connecting to server...");Socketconnection=server.accept();System.out.println("Connection established.");ObjectOutputStreamclientOut=newObjectOutputStream(client.getOutputStream());ObjectOutputStreamserverOut=newObjectOutputStream(connection.getOutputStream());ObjectInputStreamclientIn=newObjectInputStream(client.getInputStream());ObjectInputStreamserverIn=newObjectInputStream(connection.getInputStream());System.out.println("Communication is ready.");StringmessageOut="Hello World";}}
- Data streams Using data streams, serialization is done by converting objects into primitive data types or a
Send the message. Write data to the output stream and flush the stream to ensure the data has been written entirely.
- Data streams Length of a message needs to be sent first so the other party knows how many bytes it needs to read. After the length is sent as a primitive integer type, bytes can be sent.
importjava.io. DataInputStream;importjava.io. DataOutputStream;importjava.net. InetAddress;importjava.net. ServerSocket;importjava.net. Socket;publicclassNetworkAppExample{publicstaticvoidmain(String[]args)throwsException{Stringhost="localhost";intport=10430;ServerSocketserver=newServerSocket(port,50,InetAddress.getByName(host));System.out.println("Server started.");Socketclient=newSocket(host,port);System.out.println("Connecting to server...");Socketconnection=server.accept();System.out.println("ConnectionFAQ
What is Server: How to Create a Network Application in Java about?
It provides a structured overview of server, explains the main context, and highlights practical takeaways for readers.
Why does this topic matter?
Understanding the main concepts helps readers evaluate the issue, avoid common mistakes, and make better-informed decisions.
How should readers use this information?
Use the guidance as a practical starting point, confirm details that may have changed, and follow current product, safety, or security recommendations.
- Data streams Length of a message needs to be sent first so the other party knows how many bytes it needs to read. After the length is sent as a primitive integer type, bytes can be sent.

Reader Comments 0
Sign in with email or Google to join the discussion.