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

📄 chatclient.java

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

public class chatClient
{
     public static void main(String[] str)
     {
        /* String serverName="";
         if (str.length>=1)serverName=str[0];
         else {
             System.out.println("You must provide a server Name you want to connect to,");
             System.out.println("And run your program by input in Dos Prompt Like this:");
             System.out.println("java chatClient serverName");
             System.exit(0);
         }*/
         Socket cltSoc;
         ObjectOutputStream objOut=null;
         ObjectInputStream objIn=null;
         try{
             //InetAddress address=InetAddress.getByName(serverName);
             cltSoc=new Socket("127.0.0.1",6666);
             objOut=new ObjectOutputStream(cltSoc.getOutputStream());
             objIn=new ObjectInputStream(cltSoc.getInputStream());
             System.out.println("Connected to the server successfully...");
             System.out.println("If you want to exit this program, type 'QUIT' !");
            
         }catch(Exception e){System.exit(0);}
        
         getMsg get=new getMsg(objIn);
         get.start();
         sendMsg send =new sendMsg(objOut);
         send.start();
     }
}
class getMsg extends Thread
{

     ObjectInputStream objIn;
     public getMsg(ObjectInputStream in)
     {
         objIn=in;
     }
     public void run()
     {
         String strMsg="";
         while(true)
         {
             try{
             strMsg=(String)objIn.readObject();
             System.out.println("The server said:"+strMsg);
             if (strMsg.equals("QUIT"))System.exit(0);
            
             }catch(Exception e){}
         }
     }
}
class sendMsg extends Thread
{
     ObjectOutputStream objOut;
     public sendMsg(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);
         }
     }
    
}

⌨️ 快捷键说明

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