12
2010
Making a Client Server application (Socket programming) in java
The programming languages become handier when the user can create a link between two pieces of compiled codes operating from two different locations.
In this example, a simple server that takes a text string from the client and returned in upper case is discussed. Hope this will give an overall picture about how socket programming work in JAVA.
You do not have to be an expert in Java and all you need is a Java compiler.
Let us get started;
- Create an empty folder in your C: drive with the name “Tutebox”
- Create 2 empty JAVA files inside the folder and rename them as:
- TCPServer.java
- TCPClient.java
- Then copy/paste the following codes into those files and save them.
- Compile the java files.
- Open command prompt
- Type “C:” {enter}
- Type “cd C:\Tutebox” {enter}
- Type “javac *.java” {enter}
- If you have returned to the command prompt, you are ready to run your client server application. (If you do have difficulties please write to us, we would be happy to help you)
or you can simply download TCPClient.java and TCPServer.java from here.
Server side code
import java.io.*;
import java.net.*;
class TCPServer {
public static void main(String argv[]) throws Exception {
String clientSentence;
String capitalizedSentence;
ServerSocket welcomeSocket = new ServerSocket(6789);
while(true) {
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
System.out.println(“Server waiting for data…”);
clientSentence = inFromClient.readLine();
System.out.println(“Recived from client : ” + clientSentence);
capitalizedSentence = clientSentence.toUpperCase() + ‘\n’;
outToClient.writeBytes(capitalizedSentence);
}
}
}
Client Side code
import java.io.*;
import java.net.*;
class TCPClient {
public static void main(String argv[]) throws Exception {
String sentence;
String modifiedSentence;
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
Socket clientSocket = new Socket(“localhost”, 6789);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
System.out.print(“SEND TO SERVER: “);
sentence = inFromUser.readLine();
outToServer.writeBytes(sentence + ‘\n’);
modifiedSentence = inFromServer.readLine();
System.out.println(“FROM SERVER: ” + modifiedSentence);
clientSocket.close();
}
}
You have just created a client server application. Now let us see how it works in real time.
- Open two command prompts;
- First command prompt
- Type “C:”
- Type “cd C:\Tutebox”
- Type “java TCPServer”
- Second command prompt
- Type “C:”
- Type “cd C:\Tutebox”
- Type “java TCPClient”
You are ready to go.
Write some text in any case on the client program and press enter.
Your server, which is running on the other command prompt, will convert it to uppercase and send it back to the clients’ command prompt.
“Knowledge belongs to world and Be proud to pass it on” – Tutebox
Related Posts
11 Comments + Add Comment
Recent Posts
- How to populate a dropdown menu from database in CodeIgniter [Workaround]
- How to Overclock Samsung Galaxy Mini Upto 800MHz
- Current and Future Relavance of Business Excellence / Improvement to Argos UK Ltd. Possible Benefits and Likely Inhabitors Exists
- What can Harm Your Online Reputation?
- Does Race Matter in the Choice of Popular Music among Youngsters?
The Tutebox
- Accounting
- Android Tips
- Apps
- Business
- C / C++
- Computers
- Database
- Design Patterns
- E-Commerce
- Economics
- Electronics
- Entertainment
- Finance
- Functional Programming
- Games
- Hardware
- Health
- HRM
- International Business
- Internet
- iOS Tips
- JAVA
- Life Style
- Linux
- M-Commerce
- Management
- Marketing
- Mathematics
- Mobile
- MS Office
- Network
- PC
- Personal Development
- Photography
- PHP
- Physics
- Places
- Programming
- Science
- Software
- Statistics
- Tweaks
- Visual Studio
- Web Hosting
- Windows

Article by






Nice to have you back. And again with the interesting posting.
useful article.. keep writing.. thank you!!
hey, good job as always genius.
# * **** * #
Thanks alot CuteGirl
it was very interesting to read http://www.tutebox.com
I want to quote your post in my blog. It can?
And you et an account on Twitter?
Nice post… keep writing…
I really love the topic that you shared. Thank you for posting.
I have doubt ..
scenario -> we have 2 clients and server.
Client A has connected with Server… but message is not sent yet!
Client B is connecting and sending message..
if i close the A what will happen to Client B’s Message ?
How to manage these kind a problems???
Easiest solution for these kind of scenario is to choose different ports to communicate..
Nice……keep it ON!!1
I want to play my Chess application in LAN,which developed in java…………
will u plz help??