GetMessage服务端
import java.io.BudderedRead;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import .ServerSocket;
import .Scoket;
import javax.swing.JLable;
import javax.swing.JTexArea;
public class GetMessage extends Thread{
private int i;
String v;
JLable lable=null;
private JTexArea;
public GetMessage(int i,JTextArea text){
this.i=i;
this.text=text;
}
public void run(){
try{
ServerSocket so=new ServerSocket(i);
Socket s=so.accept();
while(trun){
InputStreamReader i=new InputStreamReader(s.getInputStream());
BufferedReader b=new BufferedReader(i);
v=b.readLine;
text.append("对方说"+v+"\n");
}
}catch(IOExeception e){
//labled.setText("对方已经下线");
text.append("对方下线了。。。");
}
}
}
SendMessage 客服端
import java.io.BufferedReader;
import java.io.IOException;
import java.Io.InputstreamReader;
inport java.io.PrintStream;
import .Socket;
import .UnknownHostException;
import javax.swing.JLable;
import javax.swing.JTextArea;
import javax.swing.JTextFileld;
public class SenfMessage extend Thread{
private String ip;
private int i;
Socket s=null;
JLabel label=null;
JTextFileld text;
JTexArea text1;
public SendMessage(String ip,int i,JTextArea text1){
this.ip=ip;
this.i=i;
this.text1=text1;
}
public void run(){
while(trun){
try{
s=new Socket(ip,i);
text1.setText("连接成功"+"\n")
break;
}catch(Exception e){
try{
Thread.sLeep(1000);
}catch(InterruptedException e1){
System.out.println("出错了。。。");
}
}
}
}
public void send(String message){
{
try{
printStream p=new PrintStream(s.getOutputStream());
p.println(message);
}catch(Exception e1){
System.out.println("异常"+e1.getMessage());
}
}
}
Test测试类
import java.awt.*;import java.awt.event.*;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.*;import javax.swing.*;
import javax.swing.event.*;
class WindowTextAtrea extenfs JFrame importlements ActionListener
{
String s;
JTextArea text1;
JTextArea text2;
JButton button1,button2;button3;
SendMessage t2;
GetMessage t1;
JLabel lable1,Lable2;
JTextField text;
WindowTextAtrea();
{
this.s=s;
lable1=new JLable("对方ip");
text=new JTextField(20);
text1=new JTextArea(6,18);
text2=new JTextArea(6,18);
text2.setEditable(false);
button1=new JButton("发送");
button2=new JButton("关闭");
button3=new JButton("确定ip");
setBounds(100,100,450,300);
setVisible(true);
Countainer con=getContenPane();
con.setLayout(new FlowLayout());
con.add(lable1);
conn.add(text);
con.add(button3);
con.add(new JScrollPane(text1));
con.add(new JScrollPane(text2));
con.add(button1);
c
on.add(button2);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
con.validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void runthread(String ip)
{
t1=new GetMessage(4066,text2);
t1.start();
t2=new SendMessage(ip,4066,text2);
t2.start();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button2){
System.exit(0);
}
}
if(e.getSource()==button1)
{
text2.append(text1.getText()+"\n");
t2.send(text1.getText());
text1.setText("");
}
if(e.getSource()==button3)
{
s=text.getText();
runthread(s);
}
}
}
public class Test{
public static void main(String[] args){
new WindowTextArea();
}
}