zoukankan      html  css  js  c++  java
  • Socket编程小例子

    socket编程客户端例子:

    package com.gx.chat;

    import java.awt.BorderLayout;
    import java.awt.Frame;
    import java.awt.TextArea;
    import java.awt.TextField;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.net.Socket;
    import java.net.UnknownHostException;

    /**
    * @author <a href="mailto:gaoxiang114@163.com">hover</a>
    * @version 1.0.0.2014年4月8日
    */
    public class CharClient extends Frame{

    TextArea textArea = null;
    TextField textField = null;
    Socket socket = null;
    DataOutputStream os = null;

    public CharClient(){
    setLocation(400, 300);
    textArea = new TextArea();
    textField = new TextField();
    this.setSize(400, 400);
    this.add(textArea,BorderLayout.NORTH);
    this.add(textField,BorderLayout.SOUTH);
    pack();
    this.addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent e){
    System.exit(0);
    }
    });
    textField.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
    String msg = textField.getText();
    textArea.setText(msg);
    textField.setText("");
    try {
    os.writeUTF(msg);
    os.flush();
    } catch (IOException e1) {
    e1.printStackTrace();
    }
    }
    });
    this.setVisible(true);
    connect();
    }

    public static void main(String[] args) {
    new CharClient();
    }

    public void connect(){
    try {
    socket = new Socket("127.0.0.1",9999);
    os = new DataOutputStream(socket.getOutputStream());
    } catch (UnknownHostException e) {
    e.printStackTrace();
    } catch (Exception e) {
    e.printStackTrace();
    try {
    if(os != null){
    os.close();
    }
    if(socket != null){
    socket.close();
    }
    } catch (IOException e1) {
    e1.printStackTrace();
    }
    }
    }
    }

     服务端例子:

    package com.gx.chat;

    import java.io.DataInputStream;
    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;

    /**
    * @author <a href="mailto:gaoxiang114@163.com">hover</a>
    * @version 1.0.0.2014年4月8日
    */
    public class ChatServer {

    ServerSocket serverSocket = null;
    Socket socket = null;
    boolean flag = false;
    DataInputStream os = null;
    public static void main(String[] args) {
    new ChatServer().start();
    }

    public void start(){
    try {
    serverSocket = new ServerSocket(9999);
    } catch (IOException e) {
    e.printStackTrace();
    System.exit(0);
    }

    flag = true;

    try {
    while(flag){
    boolean isConnect = false;
    socket = serverSocket.accept();
    System.out.println("一个客户端连接");
    new Thread(new ChatThread(socket)).start();;
    }

    } catch (IOException e) {
    try {
    os.close();
    } catch (IOException e1) {
    e1.printStackTrace();
    }
    }

    }

    class ChatThread implements Runnable{
    Socket socket = null;
    public ChatThread(Socket socket){
    this.socket = socket;
    }
    boolean isConnect = false;
    @Override
    public void run() {
    isConnect = true;
    while(isConnect){
    try {
    os = new DataInputStream(socket.getInputStream());
    System.out.println(os.readUTF());
    } catch (IOException e) {
    try {
    os.close();
    } catch (IOException e1) {
    e1.printStackTrace();
    }
    }
    }
    }
    }
    }

  • 相关阅读:
    献给 Python 开发人员的 25 个最佳 GitHub 代码库!
    6 drf-认证权限解析频率
    4: drf视图组件
    3 drf请求与响应
    十三:Django框架学习之auth模块
    十二:django中间件学习之拓展1
    git光速从入门到精通
    mac系统下的mysql安装(ver.8.0.20)
    python小练习——2048
    docker下overlay2占用空间过大,清理docker占用空间
  • 原文地址:https://www.cnblogs.com/gaoxiang116/p/3652299.html
Copyright © 2011-2022 走看看