socket program example!
In this program, I develop an socket server running at port 5000.
Then I develop a Application client and a Applet ClIEnt program to
connect to the server and receive a message.This is very simple but
if import usage. We can alse transfer an serializable object in two
application by the same method.If you want to know more let me know
down load source
1. Server program source
// Server.Java
/**
* This a Socket Server program
* @version 1.0 1999/12/27
* @author Huang Jian-chang
**/
import Java.Net.*;
import Java.io.*;
public class Server
{
ServerSocket server;
DataOutputStream output;
Socket socket;
public Server (){
try{
// create a server on port 5000
server=new ServerSocket(5000);
// display interactive informaion
System.out.println("Server created.");
System.out.println("waiting for clIEnt to connect on...");
// waiting for clIEnt to connect on...
socket = server.accept();
// clIEnt connected
System.out.println("clIEnt connected.\nShutdown!");
output = new DataOutputStream(socket.getOutputStream());
output.writeUTF("Welcome to Server.Bye!");
output.close();
server.close();
}
catch(SocketException e){
System.out.println(e.toString());
e.printStackTrace();
System.exit(1);
}
catch(IOException e){
System.out.println(e.toString());
e.printStackTrace();
System.exit(1);
}
}
public static void main(String args[]){
Server game=new Server();
}
}
2. Application ClIEnt program source
// ClIEnt.Java
/**
* This a Socket ClIEnt program
* @version 1.0 1999/12/27
* @author Huang Jian-chang
**/
import Java.io.*;
import Java.Net.*;
public class ClIEnt {
public static void main(String args[]) {
try{
if (args.length != 1){
System.out.println("USAGE: Java ClIEnt servername");
return;
}
String connectto= args[0];
Socket connection;
// connect to server
if(connectto.equals("localhost")){
connection=new Socket(InetAddress.getLocalHost(),5000);
}
else{
connection=new Socket(InetAddress.getByName(connectto),5000);
}
DataInputStream input=new DataInputStream(connection.getInputStream());
// read information from server
String info;
info = input.readUTF();
System.out.println(info);
connection.close();
}
catch(SecurityException e){
System.out.println("SecurityException when connecting Server!");
}
catch(IOException e){
System.out.println("IOException when connecting Server!");
}
}
}
3. Applet ClIEnt program source
// AppletClIEnt.Java
/**
* This a Socket Applet ClIEnt program
* @version 1.0 1999/12/27
* @author Huang Jian-chang
**/
import Java.applet.*;
import Java.awt.*;
import Java.io.*;
import Java.Net.*;
public class AppletClIEnt extends Applet {
String info="";
public void init(){
try{
String connectto = getCodeBase().getHost();
Socket connection;
// connect to server
if(connectto.equals("localhost")){
connection=new Socket(InetAddress.getLocalHost(),5000);
}
else{
connection=new Socket(InetAddress.getByName(connectto),5000);
}
DataInputStream input=new DataInputStream(connection.getInputStream());
// read information from server
info = input.readUTF();
System.out.println(info);
connection.close();
}
catch(SecurityException e){
System.out.println("SecurityException when connecting Server!");
info = e.toString();
}
catch(IOException e){
System.out.println("IOException when connecting Server!");
info = e.toString();
}
}
public void paint(Graphics g){
g.drawString(info,20,20);
}
}
Html File AppletClIEnt.Html :
code=AppletClIEnt.class
width = 200
height = 200
>
4 steps to run the application
1. javac Server.Java
2. javac ClIEnt.Java
3. Java Server
4. Java ClIEnt localhost(or the computername in which the server is running)
! do the step 3 and 4 in two diffrent computer or DOS Commnand