不知道怎么才能实现广域网
解决方案 »
- File对象的list方法的使用问题:
- 如何使用线程让时间会动!
- 关于截图的思路?
- 求救,昨天还能用,今天用不了了!!急急急(我刚学,比较菜,不要笑我)
- 一个使用java包的简单问题
- 需要一个记事本式的程序的源程序
- Is this program wrong?
- 我怎么取得session中的每个变量的值及session的长度,请赐教!谢谢!非常急用!高分!请详细一点,一定给分!
- 大家考SCJP的时候都用的什么书?现在市面上有三本书,谁用过的话,评价一下吧!谢谢!
- 国内那有 installanywhere 下载!?
- demo中的例子,jfc\c\SwingSet2\src中SwingSet2.java中有一句很怪的语句
- 怎样调用一个类的私有方法
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.lang.*;
public class CClientu extends JFrame implements ActionListener,Runnable
{
TextField tf;
TextArea ta;
Socket s;
DataInputStream dis;
DataOutputStream dos;
Thread t;
CClientu()
{
tf=new TextField();
tf.setEditable(false);
tf.addActionListener(this);
ta=new TextArea();
t=new Thread(this);
getContentPane().add(tf,BorderLayout.NORTH);
getContentPane().add(ta,BorderLayout.CENTER);
setSize(220,150);
setLocation(500,0);
setTitle("Client");
validate();
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
try{
s=new Socket("127.0.0.1",2008);
dos=new DataOutputStream(s.getOutputStream());
dis=new DataInputStream(s.getInputStream());
tf.setEditable(true);
t.start();
}catch(Exception ex){System.out.println("Setup connect failed...");}
} public void actionPerformed(ActionEvent e)
{
if(!tf.getText().equals(""))
{
try{
dos.writeUTF("Client:"+tf.getText()+'\n');
ta.appendText("Client:"+tf.getText()+'\n');
tf.setText("");
}catch(Exception e4){System.out.println("Send data failed...");}
}
} public void run()
{
do{
try{
if(dis.available()!=0) ta.appendText(dis.readUTF());
Thread.sleep(200);
}catch(Exception e3){System.out.println("Receive data failed...");}
} while(true);
} public static void main(String args[])
{ new CClientu(); }}
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.lang.*;
public class CServeru extends JFrame implements ActionListener,Runnable
{
TextField tf;
TextArea ta;
Container con;
ServerSocket waitSocketConnection;
Socket client;
DataInputStream dis;
DataOutputStream dos;
Thread rcv;
CServeru()
{
tf=new TextField();
tf.setEditable(false);
ta=new TextArea();
rcv=new Thread(this);
tf.addActionListener(this);
con=getContentPane();
con.add(tf,BorderLayout.NORTH);
con.add(ta,BorderLayout.CENTER);
setTitle("Server");
setSize(220,150);
validate();
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
try{
waitSocketConnection=new ServerSocket(2008);
}catch(IOException e2){System.out.println("Setup ServerSocket error...");}
try{
client=waitSocketConnection.accept();
dis=new DataInputStream(client.getInputStream());
dos=new DataOutputStream(client.getOutputStream());
tf.setEditable(true);
rcv.start();
}catch(Exception e1){System.out.println("Setup I/O failed...");}
}
public void actionPerformed(ActionEvent e)
{
if(!tf.getText().equals(""))
{
try{
dos.writeUTF("Server:"+tf.getText()+'\n');
ta.appendText("Server:"+tf.getText()+'\n');
tf.setText(""); }catch(Exception e4){System.out.println("Send data failed...");}
}
} public void run()
{
do{
try{
if(dis.available()!=0) ta.appendText(dis.readUTF());
Thread.sleep(200);
}catch(Exception e3){System.out.println("Receive data failed...");}
}while(true);
} public static void main(String args[])
{ new CServeru(); }
}