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

📄 client1.java

📁 java类似与qq的服务端原代码
💻 JAVA
字号:
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class client1 extends JFrame
{ private JTextField enter;
  private JTextArea display;
   PrintStream output;
   DataInputStream input;
   String message="";
   public client1()
   {super("Client");
    Container c=getContentPane();
    enter=new JTextField();
    enter.addActionListener(new ActionListener()
    { public void actionPerformed(ActionEvent e)
    {sendData(enter.getText());
    }
    });
    c.add(enter,BorderLayout.NORTH);
    display=new JTextArea();
    c.add(new JScrollPane(display),BorderLayout.CENTER);
    setSize(500,500);
    show();
}
public void connect()
{Socket socket;
try{
	display.setText("准备连接.....");
	socket=new Socket(InetAddress.getByName(""),4321);
	display.append("连接到:"+socket.getInetAddress().getHostName());
	display.append("\n主机IP为:"+socket.getInetAddress().toString());
	output=new PrintStream(new BufferedOutputStream(socket.getOutputStream()));
	output.flush();
	input=new DataInputStream(new BufferedInputStream(socket.getInputStream()));
	enter.setEnabled(true);
	do{
		try{
			message=((String)input.readLine());
			display.append("\n"+message);
			}
			catch(IOException e)
			{display.append("\n 无法连接获得信息");
			}
         }
         while(!message.equals("Server:end"));
         display.append("\n关闭连接");
         output.close();
         input.close();
         socket.close();
} 
catch(EOFException eof)
{System.out.println("服务器中断");
} 
catch (IOException e)
{e.printStackTrace();
}      
   }
   private void sendData(String s)
   {try{message=s;
   output.println("Client: "+s);
   output.flush();
   enter.setText("");
   
   }
   catch(Exception e)
   {display.append("\n数据传输错误");
   
   }
   }
   public static void main(String args[])
   {client1 app=new client1();
     app.addWindowListener(new WindowAdapter()
     {public void windowClosing(WindowEvent e)
     {System.exit(0);
     }
     	
     });
     app.connect();
     
	}
}

⌨️ 快捷键说明

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