Server 端:
1. ServerSocket s = new ServerSocket(PORT);// 初始化一个 ServerSocket 对象
2. Socket socket = s.accept();//accept()函数阻塞等待一个 socket 连接,有连接进来 (就是 client 调用 Socket(addr,port) 的时候)会返回一个 socket 对象。
在 accetp 返回的时候,会返回一个当前连接对应的 socket 对象。
3. 通过 socket.getInputStream() 等,获取输入或者输出流进行操作
Client 端:
1. InetAddress addr = InetAddress.getByName(xx) 等获取服务器地址
2. Socket socket= new Socket(addr,PORT);// 根据地址和 port,初始化一个 socket 对象。
如果服务器没有 Socket.accept 等待连接,则返回一个异常 (Connection refused: connect)
单用户
Server 端
- import java.io.BufferedWriter;
- import java.io.IOException;
- import java.io.OutputStreamWriter;
- import java.net. * ;
- import java.io. * ;
- import javax.net.ServerSocketFactory;
- public class java_socket_server {
- public static final int port = 8080;
- public static void main(String[] args) {
- try { //可以直接用ServerSocket也可以用ServerSocketFactory ServerSocket s= null; //new ServerSocket(port); ServerSocketFactory sf = ServerSocketFactory.getDefault(); s = sf.createServerSocket(port); System.out.println("Server start to accept"); Socket socket = s.accept(); System.out.print("connection accepted:" + socket); System.out.println(" "); Process list; BufferedReader in = new BufferedReader( new InputStreamReader( socket.getInputStream()) ); PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream()) ) ,true); while(true){ String str = in.readLine(); if(str.equals("END")) { System.out.println("END received"); break; } System.out.println("Echoing:" + str); out.println(str+" fromeServer"); } System.out.println("closing...."); socket.close(); s.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}
Client 端
- import java.io.IOException;
- import java.net. * ;
- import java.io. * ;
- import java.util. * ;
- public class java_socket_client {
- public static void main(String[] args) {
- InetAddress addr;
- try {
- addr = InetAddress.getByName(null);
- System.out.println("addr " + addr);
- Socket socket1;
- Socket socket2;
- try {
- socket1 = new Socket(addr, 8080);
- System.out.println("socket 1 = " + socket1);
- BufferedReader in1 = new BufferedReader(new InputStreamReader(socket1.getInputStream()));
- PrintWriter out1 = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket1.getOutputStream())), true);
- for (int i = 0; i < 10; i++) {
- out1.println("howdy" + i);
- String str = in1.readLine();
- System.out.println(str);
- }
- out1.println("END");
- socket1.close();
- } catch(IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}
Server 端的 log:
- Server start to acceptconnection accepted:Socket[addr=/127.0.0.1,port=64048,localport=8080] Echoing:howdy0Echoing:howdy1Echoing:howdy2Echoing:howdy3Echoing:howdy4Echoing:howdy5Echoing:howdy6Echoing:howdy7Echoing:howdy8Echoing:howdy9END receivedclosing....
Client 端 log
- addr localhost/127.0.0.1socket 1 = Socket[addr=localhost/127.0.0.1,port=8080,localport=64048]socket 2 = Socket[addr=localhost/127.0.0.1,port=8080,localport=64049]howdy0 fromeServerhowdy1 fromeServerhowdy2 fromeServerhowdy3 fromeServerhowdy4 fromeServerhowdy5 fromeServerhowdy6 fromeServerhowdy7 fromeServerhowdy8 fromeServerhowdy9 fromeServer
多用户
Server 端:
每有一个连接过来就创建一个进行通信
- //: MultiJabberServer.java// A server that uses multithreading to handle// any number of clients.import java.io.*;import java.net.*;class ServeOneJabber extends Thread { private Socket socket; private BufferedReader in; private PrintWriter out; public ServeOneJabber(Socket s) throws IOException { socket = s; in = new BufferedReader( new InputStreamReader( socket.getInputStream())); // Enable auto -flush: out = new PrintWriter( new BufferedWriter( new OutputStreamWriter( socket.getOutputStream())), true); // If any of the above calls throw an // exception, the caller is responsible for // closing the socket. Otherwise the thread // will close it. start(); // Calls run() } public void run() { try { while (true) { String str = in.readLine(); if (str.equals("END")) break; System.out.println("Echoing: " + str); out.println(str); } System.out.println("closing..."); } catch (IOException e) { } finally { try { socket.close(); } catch(IOException e) {} } }}public class MultiJabberServer { static final int PORT = 8080; public static void main(String[] args) throws IOException { ServerSocket s = new ServerSocket(PORT); System.out.println("Server Started"); try { while(true) { // Blocks until a connection occurs: Socket socket = s.accept(); try { new ServeOneJabber(socket); } catch(IOException e) { // If it fails, close the socket, // otherwise the thread will close it: socket.close(); } } } finally { s.close(); } }} ///:~
就爱阅读 www.92to.com 网友整理上传, 为您提供最全的知识大全, 期待您的分享,转载请注明出处。
来源: