Java Socket编程

来源:互联网 发布:诺基亚5230软件 编辑:程序博客网 时间:2024/04/29 05:14
<script type="text/javascript">google_ad_client = "pub-8800625213955058";/* 336x280, 创建于 07-11-21 */google_ad_slot = "0989131976";google_ad_width = 336;google_ad_height = 280;//</script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>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 knowdown 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 informaionSystem.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 serverif(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 serverif(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 :
<html><head><title>Untitled Document</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312"></head><body bgcolor="#FFFFFF"><hr><applet code=AppletClient.classwidth = 200height = 200></applet> <hr></body</html>
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
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 精忠岳飞刘承俊 刘承司 刘承宇 刘承祐 刘承羽 刘承羽白色内裤 刘承羽图片 刘承羽比基尼 攻守道刘承羽真空 刘招华 鲁豫采访刘招华 采访刘招华一句经典台词 刘招华三个老婆的照片 刘的拼音 刘拼音怎么写 刘字拼音 刘拼音怎么拼 刘拼音怎么写的 刘的拼音怎么写 刘的汉语拼音 刘怎么拼音 刘振东 北戴河刘振东旅馆 刘振亚 刘振华 刘振华将军 刘振华上将 刘振国 刘振声 刘振起 王梦瑶刘振阳事件 公刘墓 刘放吾 刘政委 刘效松 刘效松老婆 刘敏涛个人资料 刘敏涛图片 刘敏涛老公 陈平 教授 汪祖生教授