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

📄 chatsvr.java

📁 java,关于tcp协议的网络连接.[采用c/s模式]
💻 JAVA
字号:
import java.io.*;
import java.net.*;

public class chatSvr
{
     public static void main(String[] str)
     {
         ServerSocket soc;
         Socket svrSoc;
         ObjectOutputStream objOut=null;
         ObjectInputStream objIn=null;
             try{
                 System.out.println("Waiting for client's connecting...");
                 soc=new ServerSocket(6666);
                 svrSoc=soc.accept();
                 System.out.println("The client connected, you can Exit this program by type 'QUIT'");
                
                 objOut=new ObjectOutputStream(svrSoc.getOutputStream());
                 objIn=new ObjectInputStream(svrSoc.getInputStream());
             }catch(Exception e){System.exit(0);}

        
         sendMsgOut send=new sendMsgOut(objOut);
         send.start();
         getMsgFromClient get=new getMsgFromClient(objIn);
         get.start();
     }
}
class sendMsgOut extends Thread
{
     ObjectOutputStream objOut=null;
     public sendMsgOut(ObjectOutputStream out)
     {
         objOut=out;
     }
     public void run()
     {
         String strMsg="";
         while(true)
         {
             try{
             strMsg=(new BufferedReader(new InputStreamReader(System.in))).readLine();
             objOut.writeObject(strMsg);
             if (strMsg.equals("QUIT"))System.exit(0);
             }catch(Exception e){}
             //System.out.println(strMsg);
         }
     }
    
}
class getMsgFromClient extends Thread
{
     ObjectInputStream objIn;
     public getMsgFromClient(ObjectInputStream in)
     {
         objIn=in;
     }
     public void run()
     {
         String strMsg="";
         while(true)
         {
             try{
             strMsg=(String)objIn.readObject();
             System.out.println("The client said:"+strMsg);
             if (strMsg.equals("QUIT"))System.exit(0);
             }catch(Exception e){}
         }
     }
}

⌨️ 快捷键说明

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