yylchatclient.java

来自「使用Java实现的基于TCP协议的通信的小的聊天程序的客户端」· Java 代码 · 共 75 行

JAVA
75
字号

import java.io.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.net.*; 

import javax.swing.JFrame;

public class YylChatClient extends Frame 
{ 
 

TextField tf=new TextField(20); 
TextArea ta=new TextArea(); 
Socket client; 
InputStream in; 
BufferedReader br; 
OutputStream out; 
BufferedWriter bw; 
public YylChatClient() 
{ 
super("客户端"); 
add("North",tf); 
add("Center",ta); 
setSize(400,400); 
show(); 
try 
{ 
client=new Socket("localhost",8189); 
ta.append("Connect to:"+client.getInetAddress().getHostName()+"\n\n"); 
in=client.getInputStream(); 
br=new BufferedReader(new InputStreamReader(in)); 
out=client.getOutputStream(); 
bw=new BufferedWriter(new OutputStreamWriter(out)); 
} 
catch(IOException ioe){} 
while(true) 
{ 
try 
{ 
byte[] buf=new byte[200]; 
in.read(buf); 
String str=new String(buf); 
ta.append("杨叶龙 说:"+str); 
ta.append("\n"); 
} 
catch(IOException e){} 
} 
} 
public boolean action(Event e,Object o) 
{ 
try 
{ 
String str=tf.getText(); 
byte[] buf=str.getBytes(); 
tf.setText(null); 
out.write(buf); 
ta.append("客户端 说:"+str); 
ta.append("\n"); 
} 
catch(IOException ioe){} 

return true; 

} 
public static void main(String args[]) 
{ 
	YylChatClient yyl=new YylChatClient(); 
    yyl.dispose();
    System.exit(0);

} 

} 

⌨️ 快捷键说明

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