//以下是客户端的APPLET程序
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;public class NetClient extends Applet implements ActionListener
{
String errorMsg;
Label title;
TextArea show;
Button button;
Socket client=null;
DataInputStream dis=null;
DataOutputStream dos=null;
public void init()
{
try
{
client=new Socket("192.168.1.3",19840);
dis=new DataInputStream(client.getInputStream());
dos=new DataOutputStream(client.getOutputStream());
}
catch(UnknownHostException e)
{
errorMsg+=e;
}
catch(IOException e1)
{
errorMsg+=e1;
}

setBackground(Color.WHITE);
setLayout(new BorderLayout());
title=new Label("这是一个客户端界面");
title.setFont(new Font("楷体",Font.BOLD,18));
add("North",title);
show=new TextArea(errorMsg,10,30);
add("Center",show);
button=new Button("发 送");
add("South",button);
}

public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==button)
{
try
{
dos.writeUTF(show.getText());
}
catch(IOException e)
{ }
}
}
}
//以下是服务器端的程序
import java.net.*;
import java.io.*;public class NetServer 
{
public static void main(String[] args)
{
try
{
ServerSocket server=new ServerSocket(19840,5);
Socket socket=null;
while(true)
{
socket=server.accept();
new Server_thread(socket);
}
}
catch(IOException e)
{
System.out.println("ERROR:"+e);
}
}}
class Server_thread implements Runnable
{
private Thread t;
private static int threadName=0;
private static Thread threadGroup[]=new Thread[5];
private DataInputStream input=null;
private DataOutputStream output=null;
public Server_thread(Socket socket)
{
threadName++;
t=new Thread(this,String.valueOf(threadName));
fill(t);
try
{
input=new DataInputStream(socket.getInputStream());
output=new DataOutputStream(socket.getOutputStream());
}
catch(IOException e)
{
System.out.println("网络中断...");
}
t.start();
}
private synchronized boolean fill(Thread t)
{
System.gc();
for(int i=0;i<5;++i)
{
if(threadGroup[i]!=null)
{
threadGroup[i]=t;
return true;
}
}
return false;
}
public void run()
{
try
{
System.out.println(input.readUTF());
}
catch(IOException e)
{
System.out.println("客户端离开");
}
try
{
output.writeUTF(new String("你好啊!"));
}
catch(IOException e)
{
System.out.println(e);
}
}
}
问题:
我是想在APPLET程序里点击按钮,把TEXT里的内容发送到服务器那里,然后服务器在控制台输出这句话,再发回客户端一句话!
现在有两个问题一个是服务器端程序不能在控制台输出那句话?
第二个把服务器发回到客户端的信息怎么显示在TextArea里啊?(也就是唯一,一个TextArea控件中)

解决方案 »

  1.   

    1) flush 或 close
    2) 客户端 socket=server.accept(); 中得到getInputStream(),得到服务器返回值
      

  2.   

    //以下是加过close的客户端的APPLET程序
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.net.*;public class NetClient extends Applet implements ActionListener
    {
    String errorMsg;
    Label title;
    TextArea show;
    Button button;
    Socket client=null;
    DataInputStream dis=null;
    DataOutputStream dos=null;
    public void init()
    {
    try
    {
    client=new Socket("192.168.1.3",19840);
    dis=new DataInputStream(client.getInputStream());
    dos=new DataOutputStream(client.getOutputStream());
    }
    catch(UnknownHostException e)
    {
    errorMsg+=e;
    }
    catch(IOException e1)
    {
    errorMsg+=e1;
    }

    setBackground(Color.WHITE);
    setLayout(new BorderLayout());
    title=new Label("这是一个客户端界面");
    title.setFont(new Font("楷体",Font.BOLD,18));
    add("North",title);
    show=new TextArea(errorMsg,10,30);
    add("Center",show);
    button=new Button("发 送");
    add("South",button);
    }

    public void actionPerformed(ActionEvent ae)
    {
    if(ae.getSource()==button)
    {
    try
    {
    dos.writeUTF(show.getText());
    dos.close();
    dis.close();
    }
    catch(IOException e)
    { }
    }
    }
    }
    //以下是加过close的服务器端程序
    import java.net.*;
    import java.io.*;public class NetServer { public static void main(String[] args)
    {
    try
    {
    ServerSocket server=new ServerSocket(19840,5);
    Socket socket=null;
    while(true)
    {
    socket=server.accept();
    new Server_thread(socket);
    }
    }
    catch(IOException e)
    {
    System.out.println("ERROR:"+e);
    }
    }}
    class Server_thread implements Runnable
    {
    private Thread t;
    private static int threadName=0;
    private static Thread threadGroup[]=new Thread[5];
    private DataInputStream input=null;
    private DataOutputStream output=null;
    public Server_thread(Socket socket)
    {
    threadName++;
    t=new Thread(this,String.valueOf(threadName));
    fill(t);
    try
    {
    input=new DataInputStream(socket.getInputStream());
    output=new DataOutputStream(socket.getOutputStream());
    }
    catch(IOException e)
    {
    System.out.println("网络中断...");
    }
    t.start();
    }
    private synchronized boolean fill(Thread t)
    {
    System.gc();
    for(int i=0;i<5;++i)
    {
    if(threadGroup[i]!=null)
    {
    threadGroup[i]=t;
    return true;
    }
    }
    return false;
    }
    public void run()
    {
    try
    {
    System.out.println(input.readUTF());
    input.close();
    }
    catch(IOException e)
    {
    System.out.println("客户端离开");
    }
    try
    {
    output.writeUTF(new String("你好啊!"));
    output.close();
    }
    catch(IOException e)
    {
    System.out.println(e);
    }
    }
    }
    修改后,还是不行啊?
    "2) 客户端 socket=server.accept(); 中得到getInputStream(),得到服务器返回值"
    对于这句话我不知道您是什么意思?您是想让我在客户端加这句,还是说我写的这句不应该在服务器端,应该在客户端?
      

  3.   

    第一个问题已经搞定了,现在就想知道:第二个把服务器发回到客户端的信息怎么显示在TextArea里啊?(也就是唯一,一个TextArea控件中)