`
javasee
  • 浏览: 926757 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

Socket 群聊

阅读更多

Server 代码

package com;

import java.net.*;
import java.io.*;
import java.util.*;

public class Server {
 public static void main(String[] args) throws Exception {
  ServerSocket server = new ServerSocket(8001);
  List<Socket> ss = new ArrayList<Socket>();
  while (true) {
   Socket socket = server.accept();
   ss.add(socket);
   new ServerThread1(socket).start();
  }
  // socket.close();
  // server.close();
 }
}

class ServerThread1 extends Thread {
 private Socket socket;

 public ServerThread1(Socket socket) {
  this.socket = socket;
 }

 public void run() {
  PrintWriter out = null;
  BufferedReader in = null;
  try {
   out = new PrintWriter(socket.getOutputStream());
   in = new BufferedReader(new InputStreamReader(socket
     .getInputStream()));

   String s = in.readLine();
   System.out.println(s + "上线了");
   out.println(s + "上线了");
   out.flush();

   s = in.readLine();
   while (s != null && !s.equals("quit")) {

    System.out.println(s);

    out.println(s);

    out.flush();

    s = in.readLine();
   }
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   try {
    in.close();
    out.close();
    socket.close();
   } catch (Exception e) {

   }
  }
 }
}

Client代码

package com;

import java.net.*;
import java.io.*;

public class Client {

 public static void main(String[] args) throws Exception {
  Socket socket = new Socket("127.0.0.1", 5001);
  //
  BufferedReader sin = new BufferedReader(
    new InputStreamReader(System.in));
  PrintWriter out = new PrintWriter(socket.getOutputStream());
  String cname = args[0];
  out.println(cname);
  out.flush();
  new ClientThread1(socket).start();//

  String c = sin.readLine();
  while (!c.equals("quit")) {
   out.println(cname + "说:" + c);
   out.flush();
   c = sin.readLine();
  }

  out.close();
  socket.close();
 }
}

class ClientThread1 extends Thread {
 private Socket socket;

 public ClientThread1(Socket socket) {
  this.socket = socket;
 }

 public void run() {
  BufferedReader in = null;
  try {
   in = new BufferedReader(new InputStreamReader(socket
     .getInputStream()));
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  while (true) {
   String s;
   try {
    s = in.readLine();
    System.out.println(s);
   } catch (IOException e) {
    // TODO Auto-generated catch block
    // e.printStackTrace();
    break;
   }
  }
 }
}

3
0
分享到:
评论
1 楼 hzxlb910 2013-03-21  
没跑起来
Exception in thread "main" java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:519)
at java.net.Socket.connect(Socket.java:469)
at java.net.Socket.<init>(Socket.java:366)
at java.net.Socket.<init>(Socket.java:180)
at com.Client.main(Client.java:10)

相关推荐

Global site tag (gtag.js) - Google Analytics