`
lenj
  • 浏览: 36864 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
最近访客 更多访客>>
社区版块
存档分类
最新评论

java socket例子

阅读更多
package test.bwl;   
  
import java.io.DataInputStream;   
import java.io.DataOutputStream;   
import java.io.IOException;   
import java.net.ServerSocket;   
import java.net.Socket;   
  
public class SocketManager {   
    /**  
     * @param args  
     * @throws IOException   
     */  
    public static void main(String[] args) {   
        SocketManager manager = new SocketManager();   
        manager.doListen();   
    }   
  
    public void doListen() {   
        ServerSocket server;   
        try {   
            server = new ServerSocket(9991);   
            while (true) {   
                Socket client = server.accept();   
                new Thread(new SSocket(client)).start();   
            }   
        } catch (IOException e) {   
            e.printStackTrace();   
        }   
  
    }   
  
    //服务器进程   
    class SSocket implements Runnable {   
  
        Socket client;   
  
        public SSocket(Socket client) {   
            this.client = client;   
        }   
  
        public void run() {   
            DataInputStream input;   
            DataOutputStream output;   
            try {   
                input = new DataInputStream(client.getInputStream());   
                output = new DataOutputStream(client.getOutputStream());   
                //   
                String listMsg = input.readUTF();   
                output.writeUTF("Recive:  " + listMsg + "    \r\n Thx...");   
                System.out.println("Recive:   " + listMsg);   
                listMsg = input.readUTF();   
                output.writeUTF("Recive Second:  " + listMsg + "    \r\n Thx...");   
                System.out.println("Recive Second:   " + listMsg);   
            } catch (IOException e) {   
                e.printStackTrace();   
            }   
        }   
    }   
  
}

import java.io.DataInputStream;   
import java.io.DataOutputStream;   
import java.io.IOException;   
import java.io.OutputStream;   
import java.net.Socket;   
import java.net.UnknownHostException;   
  
public class SocketClient {   
  
    public static void main(String[] args) {   
        Socket socket = null;   
        try {   
            socket = new Socket("127.0.0.1", 9991);   
            //向服务器端第一次发送字符串      
            OutputStream netOut = socket.getOutputStream();   
            DataOutputStream doc = new DataOutputStream(netOut);   
            DataInputStream in = new DataInputStream(socket.getInputStream());   
            //向服务器端第二次发送字符串      
            doc.writeUTF("list");   
            String res = in.readUTF();   
            System.out.println(res);   
            doc.writeUTF("bye");   
            res = in.readUTF();   
            System.out.println(res);   
            doc.close();   
            in.close();   
        } catch (UnknownHostException e) {   
            e.printStackTrace();   
        } catch (IOException e) {   
            e.printStackTrace();   
        } finally {   
            if (socket != null) {   
                try {   
                    socket.close();   
                } catch (IOException e) {   
                }   
            }   
        }   
    }   
}
分享到:
评论
1 楼 ooo456mmm 2011-11-06  
很好。不错。

相关推荐

Global site tag (gtag.js) - Google Analytics