• 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();
    }
    }
    }
    }
    }
    }

  • 相关阅读:
    node连接数据库(express+mysql)
    Apache与Tomcat有什么关系和区别
    tomcat部署简单的html静态网页
    Vue项目webpack打包部署到服务器
    windows下vue+webpack前端开发环境搭建及nginx部署
    DIV 边框重叠 像素变粗 解决办法
    基于iscroll实现下拉和上拉刷新
    css图片居中(水平居中和垂直居中)
    nodejs分页类
    事件--键盘事件
  • 原文地址:https://www.cnblogs.com/gaoxiang116/p/3652299.html
Copyright © 2020-2023  润新知