ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Socket简单实例

2021-06-02 09:52:57  阅读:158  来源: 互联网

标签:Exception Socket void 实例 str 简单 new public ta


Server端

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

public class ChatServer extends Frame
{
 TextArea ta = new TextArea();
 public void launchFrame()
 {
  add(ta, BorderLayout.CENTER);
  setBounds(0,0,200,300); 
  this.addWindowListener(
   new WindowAdapter()
   {
    public void windowClosing(WindowEvent e)
    {
     System.exit(0);
    }
   }
   );
  setVisible(true);
 }
 
 ServerSocket server = null;
 Collection cClient = new ArrayList();
 
 public ChatServer(int port) throws Exception
 {
  server = new ServerSocket(port);
  launchFrame();
 }
 
 public void startServer() throws Exception
 {
  while(true)
  {
   Socket s = server.accept();
   cClient.add( new ClientConn(s) );
   ta.append("NEW-CLIENT " + s.getInetAddress() + ":" + s.getPort());
   ta.append("\n" + "CLIENTS-COUNT: " + cClient.size() + "\n\n");
  }
 }
 
 class ClientConn implements Runnable
 {
  Socket s = null;
  public ClientConn(Socket s)
  {
   this.s = s;
   (new Thread(this)).start();
  }
  
  public void send(String str) throws IOException
  {
   DataOutputStream dos = new DataOutputStream(s.getOutputStream());
   dos.writeUTF(str);
  }
  
  public void dispose()
  {
   try {
    if (s != null) s.close();
    cClient.remove(this);
    ta.append("A client out! \n");
    ta.append("CLIENT-COUNT: " + cClient.size() + "\n\n");
   }
   catch (Exception e)
   {
    e.printStackTrace();
   }
  }
  
  public void run()
  {
   try {
    
    DataInputStream dis = new DataInputStream(s.getInputStream());
    String str = dis.readUTF();
    while(str != null && str.length() !=0)
    {
     System.out.println(str);
     for(Iterator it = cClient.iterator(); it.hasNext(); )
     {
      ClientConn cc = (ClientConn)it.next();
      if(this != cc)
      {
       cc.send(str);
      }
     }
     str = dis.readUTF();
     //send(str);
    }
    this.dispose();
   }
   catch (Exception e)
   {
    System.out.println("client quit");
    this.dispose();
   }
   
  }
 }
 
 public static void main(String[] args) throws Exception
 {
  ChatServer cs = new ChatServer(8888);
  cs.startServer();
 }
}

Client端

import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;

public class ChatClient extends Frame
{
 TextArea ta = new TextArea();
 TextField tf = new TextField();
 public void launchFrame() throws Exception
 {
  this.add(ta, BorderLayout.CENTER);
  this.add(tf, BorderLayout.SOUTH);
  tf.addActionListener(
   new ActionListener()
   {
    public void actionPerformed(ActionEvent ae)
    {
     try {
      String sSend = tf.getText();
      if(sSend.trim().length() == 0) return;
      ChatClient.this.send(sSend);
      tf.setText("");
      ta.append(sSend + "\n");
     }
     catch (Exception e) { e.printStackTrace(); }
    }
   }
   );
  
  this.addWindowListener(
   new WindowAdapter()
   {
    public void windowClosing(WindowEvent e)
    {
     System.exit(0);
    }
   }
   );
  setBounds(300,300,300,400);
  setVisible(true);
  tf.requestFocus();
 }
 
 Socket s = null;
 
 public ChatClient() throws Exception
 {
  s = new Socket("127.0.0.1", 8888);
  launchFrame();
  (new Thread(new ReceiveThread())).start();
 }
 
 public void send(String str) throws Exception
 {
  DataOutputStream dos = new DataOutputStream(s.getOutputStream());
  dos.writeUTF(str);
 }
 
 public void disconnect() throws Exception
 {
  s.close();
 }
 
 public static void main(String[] args) throws Exception
 {
  BufferedReader br = new BufferedReader (
        new InputStreamReader(System.in));
  ChatClient cc = new ChatClient();
  String str = br.readLine();
  while(str != null && str.length() != 0)
  {
   cc.send(str);
   str = br.readLine();
  }
  cc.disconnect();
 }
 
 class ReceiveThread implements Runnable
 {
  public void run()
  {
   if(s == null) return;
   try {
    DataInputStream dis = new DataInputStream(s.getInputStream());
    String str = dis.readUTF();
    while (str != null && str.length() != 0)
    {
     //System.out.println(str);
     ChatClient.this.ta.append(str + "\n");
     str = dis.readUTF();
    }
   }
   catch (Exception e)
   {
    e.printStackTrace();
   }
   
  }
 }
}

 

标签:Exception,Socket,void,实例,str,简单,new,public,ta
来源: https://blog.51cto.com/u_14943622/2844467

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有