serverskt.java

来自「利用Socket编写的一个一对一聊天的程序」· Java 代码 · 共 84 行

JAVA
84
字号
/* * serverskt.java * * Created on 2007年11月13日, 下午11:14 * * To change this template, choose Tools | Options and locate the template under * the Source Creation and Management node. Right-click the template and choose * Open. You can then make changes to the template in the Source Editor. */package 一对一聊天;import java.awt.*;import java.awt.event.*;import java.net.*;import java.io.*;/** * * @author baili */public class serverskt extends Thread{        ServerSocket skt;    Socket Client;    TextArea in;         BufferedReader theInputStream;    PrintStream theOutputStream;    String readin;        ChatOneToOne chat;    /** Creates a new instance of serverskt */    public serverskt(int port,ChatOneToOne chat) {        try        {            skt=new ServerSocket(port);            this.chat=chat;        }       catch(IOException e)        {            chat.ta.append(e.toString());        }    }    public void run()    {         try        {            chat.ta.append("等待联机......");            Client=skt.accept();             chat.ta.append("客户端"+Client.getInetAddress()+"已联机\n");             theInputStream=new BufferedReader(new InputStreamReader(Client.getInputStream()));             theOutputStream=new PrintStream(Client.getOutputStream());             while(true)             {                 readin=theInputStream.readLine();                 chat.ta.append(readin+"\n");             }        }catch(SocketException e)        {             chat.ta.append("联机中断!\n");              chat.tfaddress.setEnabled(true);                chat.tfport.setEnabled(true);                                chat.clientBtn.setEnabled(true);                chat.serverBtn.setEnabled(true);                try{                    skt.close();                    Client.close();                }                catch(IOException err)                {                    chat.ta.append(err.toString());                }        }        catch(IOException e)                {                    chat.ta.append(e.toString());                }    }    public void dataout(String data)    {        theOutputStream.println(data);    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?