Recently, I was in a position to do some Socket Programming.
From this, I learnt the following.
1. If you send any data from client Side, Its suggested to close the Client OutputStream and Socket.
2. Read the data in the stream , in the same way as how you have wrote the data in stream.
For example,
@ Client, if you are using the following approach,
OutputStream out=S.getOutputStream();
String data="testing";
PrintWriter pout=new PrintWriter(out);
pout.println(data);
out.flush();
then at the Server side, you need to use the following approach.
InputStream in = S.getInputStream();
BufferedReader bin=new BufferedReader( new InputStreamReader (in ) );
String data=bin.readLine();
if(data!=null){
System.out.println("Rx :"+data);
}
3.If you are sending data from client side, Its suggested to flush the OutputStream..
out.flush();
4. Use different name for streams, relavant to their purpose.
For example,
If you are using InputStream for reading from Keyboard,give name such as keyboardstream,
If you are using InputStream for reading from another stream, give name such as innerpipestream.
Do not use standard name like in/out , if you are using more Streams.
5. To develop a Server Program to handle multiple requests, just make the Client Socket to be handled in a Thread.
The following code will demonstrate this.
/**
MultiServer.java
**/
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
*
* @author root
*/
public class MultiServer {
public static void main(String[] args) {
try {
ServerSocket S=new ServerSocket(8888);
Socket clientsocket;
SocketHandler handle=null;
while(true){
System.out.println("Waiting for connection....");
clientsocket=S.accept();
System.out.println("Conn Status : "+ clientsocket.toString());
handle=new SocketHandler(clientsocket);
handle.start();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
/**
SocketHandler.java
**/
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
/**
*
* @author root
*/
public class SocketHandler extends Thread{
java.net.Socket S;
/** Creates a new instance of SocketHandler */
public SocketHandler(java.net.Socket s) {
this.S=s;
}
public void run(){
InputStream in=null;
OutputStream out=null;
try {
in = S.getInputStream();
out=S.getOutputStream();
BufferedReader bin=new BufferedReader( new InputStreamReader (in ) );
PrintWriter pout=new PrintWriter( out );
String data=bin.readLine();
if(data!=null){
System.out.println("Rx :"+data);
String send="thank you.. ";
pout.println(send+new java.util.Date().toString() );
pout.flush();
}
else
System.out.println("Data NULL");
S.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
From this, I learnt the following.
1. If you send any data from client Side, Its suggested to close the Client OutputStream and Socket.
2. Read the data in the stream , in the same way as how you have wrote the data in stream.
For example,
@ Client, if you are using the following approach,
OutputStream out=S.getOutputStream();
String data="testing";
PrintWriter pout=new PrintWriter(out);
pout.println(data);
out.flush();
then at the Server side, you need to use the following approach.
InputStream in = S.getInputStream();
BufferedReader bin=new BufferedReader( new InputStreamReader (in ) );
String data=bin.readLine();
if(data!=null){
System.out.println("Rx :"+data);
}
3.If you are sending data from client side, Its suggested to flush the OutputStream..
out.flush();
4. Use different name for streams, relavant to their purpose.
For example,
If you are using InputStream for reading from Keyboard,give name such as keyboardstream,
If you are using InputStream for reading from another stream, give name such as innerpipestream.
Do not use standard name like in/out , if you are using more Streams.
5. To develop a Server Program to handle multiple requests, just make the Client Socket to be handled in a Thread.
The following code will demonstrate this.
/**
MultiServer.java
**/
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
*
* @author root
*/
public class MultiServer {
public static void main(String[] args) {
try {
ServerSocket S=new ServerSocket(8888);
Socket clientsocket;
SocketHandler handle=null;
while(true){
System.out.println("Waiting for connection....");
clientsocket=S.accept();
System.out.println("Conn Status : "+ clientsocket.toString());
handle=new SocketHandler(clientsocket);
handle.start();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
/**
SocketHandler.java
**/
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
/**
*
* @author root
*/
public class SocketHandler extends Thread{
java.net.Socket S;
/** Creates a new instance of SocketHandler */
public SocketHandler(java.net.Socket s) {
this.S=s;
}
public void run(){
InputStream in=null;
OutputStream out=null;
try {
in = S.getInputStream();
out=S.getOutputStream();
BufferedReader bin=new BufferedReader( new InputStreamReader (in ) );
PrintWriter pout=new PrintWriter( out );
String data=bin.readLine();
if(data!=null){
System.out.println("Rx :"+data);
String send="thank you.. ";
pout.println(send+new java.util.Date().toString() );
pout.flush();
}
else
System.out.println("Data NULL");
S.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
No comments:
Post a Comment