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

  • 相关阅读:
    生成函数解决多重集合的计数问题
    kmp板子
    poj1001
    【题解】洛谷P1315 [NOIP2011TG] 观光公交(前缀和+贪心)
    【题解】洛谷P1941 [NOIP2014TG] 飞扬的小鸟(背包DP)
    【题解】洛谷P2679 [NOIP2015TG] 子串(DP+滚动数组)
    【题解】洛谷P1514 [NOIP2010TG] 引水入城(DFS+DP)
    【题解】洛谷P1052 [NOIP2005TG] 过河(DP+离散化)
    [arc063F]Snuke's Coloring 2-[线段树+观察]
    [agc001E]BBQ Hard[组合数性质+dp]
  • 原文地址:https://www.cnblogs.com/gaoxiang116/p/3652299.html
Copyright © 2011-2022 走看看