⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 serverskt.java

📁 利用Socket编写的一个一对一聊天的程序
💻 JAVA
字号:
/* * 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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -