VB icon

Basics of Sockets and Java

Email
Submitted on: 1/3/2015 1:30:00 AM
By: hamsters at xssi.net (from psc cd)  
Level: Beginner
User Rating: By 9 Users
Compatibility: Java (JDK 1.1), Java (JDK 1.2)
Views: 1871
 
     This is used to show a developer how to use and build Client / Server app's by using Sockets. This is very basic, and doesn't go over using UDP. Just TCP. (UDP = Datagrams.) *VOTE*
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Basics of Sockets and Java
// Description:This is used to show a developer how to use and build Client / Server app's by using Sockets. This is very basic, and doesn't go over using UDP. Just TCP. (UDP = Datagrams.) *VOTE*
// By: hamsters at xssi.net (from psc cd)
//**************************************

****************************************************
Java Code for a Client Server - w/ Sockets
****************************************************
the following code will get you started using sockets and Java:
3 steps here: 
1 - A server 
2 - A client 
3 - Data transfer 
to initialize a client socket, the code would look something like this: 
Socket clientSocket = null; 
clientSocket = new Socket(10.1.1.1,5050) 
This says that this socket is connecting to a computer with the ip: 10.1.1.1 on port 5050. Now ip could be used or a computer name. But before a client connects to a server socket, there has to be a server socket: 
Socket serverSocket = null 
serverSocket = new Socket(5050); 
//Notice no host name or ip here 
serverSocket.accept(); 
//Now accepting clients! 
From the socket you must send and recieve data. (I/O)For this to happen you need to declare a PrintWriter for output and a BufferedReader for input. 
(Client side here:) 
PrintWriter out = null; 
BufferedReader in = null; 
Socket cleintSocket = null; 
clientSocket = new Socket(10.1.1.1,5050); 
out = new PrintWriter(clientSocket.getOutputStream(), true); 
in = new BufferedReader( new inputStreamReader 
( clientSocket.getInputStream())); 
The PrintWriter out will be the front door for the socket's output. Calling out.printin("Hello") would send the string Hello to the server. 
To finish, here how to use a loop to receive information: 
PrintWriter out = null; 
BufferedReader in = null; 
Socket clientSocket = null; 
String fromServer; 
clientSocket = new Socket(10.1.1.1,5050); 
out = new PrintWriter(clientSocket.getOutputStream(), true); 
in = new BufferedReader( new inputStreamReader ( clientSocket.getInputStream())); 
while((fromServer = in.readline()) != null) { 
System.out.printin("Server: " + fromServer); 
} 
out.close(); 
in.close(); 
clientSocket.close();


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.