Socket高级编程 多客户端
客户端:
- package com.wlh.test;
- import java.io.*;
- import java.net.*;
- public class talkclient
- {
- public static void main(String args[])
- {
- try
- {
- Socket socket = new Socket("192.168.13.123",9999);
- BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));
- PrintWriter os = new PrintWriter(socket.getOutputStream());
- BufferedReader is = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- String readline;
- while (!"bye".equals(readline=sin.readLine())) {
- //==============写服务端==================//
- os.println(readline);
- os.flush();
- //==============读服务端==================//
- System.out.println("服务端反馈:"+is.readLine());
- }
- os.close();
- is.close();
- socket.close();
- }catch(Exception e)
- {
- System.out.println("Error" + e);
- }
- }
- }
package com.wlh.test; import java.io.*; import java.net.*; public class talkclient { public static void main(String args[]) { try { Socket socket = new Socket("192.168.13.123",9999); BufferedReader sin = new BufferedReader(new InputStreamReader(System.in)); PrintWriter os = new PrintWriter(socket.getOutputStream()); BufferedReader is = new BufferedReader(new InputStreamReader(socket.getInputStream())); String readline; while (!"bye".equals(readline=sin.readLine())) { //==============写服务端==================// os.println(readline); os.flush(); //==============读服务端==================// System.out.println("服务端反馈:"+is.readLine()); } os.close(); is.close(); socket.close(); }catch(Exception e) { System.out.println("Error" + e); } } }
服务端:负责接收多个客户端的连接 ,然后交给多线程去处理
- package com.wlh.test;
- import java.io.*;
- import java.net.*;
- //引入了无关的类
- import java.applet.Applet;
- public class talkserver {
- static int clientnum = 0; // 静态成员变量,记录当前客户的个数
- public static void main(String args[]) {
- ServerSocket server = null;
- try {
- server = new ServerSocket(9999);
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- while(true){
- try {
- Socket socket = server.accept();
- System.out.println("一个客户端连上来了....");
- ServerThread thread=new ServerThread(socket);
- //thread.start();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
package com.wlh.test; import java.io.*; import java.net.*; //引入了无关的类 import java.applet.Applet; public class talkserver { static int clientnum = 0; // 静态成员变量,记录当前客户的个数 public static void main(String args[]) { ServerSocket server = null; try { server = new ServerSocket(9999); } catch (IOException e1) { e1.printStackTrace(); } while(true){ try { Socket socket = server.accept(); System.out.println("一个客户端连上来了...."); ServerThread thread=new ServerThread(socket); //thread.start(); } catch (IOException e) { e.printStackTrace(); } } } }
多线程程序:
- package com.wlh.test;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.net.Socket;
- /**
- * 服务器端逻辑线程
- */
- public class ServerThread extends Thread {
- Socket socket;
- BufferedReader br=null;
- PrintWriter os=null;
- public ServerThread(Socket socket) {
- this.socket = socket;
- start(); //启动线程
- }
- public void run() {
- BufferedReader is=null;
- PrintWriter os=null;
- try {
- boolean flag=true;
- //ServerSocket server = null;
- //Socket socket = null;
- //ServerSocket server = new ServerSocket(9999);
- System.out.println("server listen on port 9999...");
- //socket = server.accept();
- is = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- os = new PrintWriter(socket.getOutputStream());
- while(flag){
- System.out.println("-------------------");
- //多线程
- //new ServerThread(socket, clientnum).start();
- //标准输入端读取一行
- BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));
- String str;
- String reply;
- while(!"".equals(str=is.readLine())){
- System.out.println(".....start...........");
- System.out.println(str); //每写一行,就等着从客户端读进一行,
- //====向客户端反馈读到的信息=====//
- os.println("服务端已经收到:"+str);
- os.flush();
- //=======从控制台读取信息==========//
- //reply=sin.readLine();
- //os.println("服务端 说:"+reply); //写到客户端
- //os.flush();
- System.out.println("......end.........");
- }
- System.out.println("out of while...");
- //String line=sin.readLine();
- os.println("server AAAAAAAAAAAAA"); //写到客户端
- os.flush();
- }
- //****流如果关闭,socket也将会关闭,所以如果想在一个socket连接之内通讯*****//,
- //***则应该在while之外关闭socket****//
- //server.close();
- } catch (Exception e) {
- System.out.println("Error" + e);
- }finally{
- try {
- is.close();
- os.close();
- socket.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- /**
- * 关闭流和连接
- */
- private void close() {
- try {
- //关闭流和连接
- os.close();
- br.close();
- socket.close();
- } catch (Exception e) {
- }
- }
- }