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(); } }
[1]
|