//以下是客户端的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控件中)
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控件中)
解决方案 »
- 构造函数是静态方法吗
- Exception in thread "main" java.lang.NullPointerException求大牛帮忙解决,30行小程序
- 求教,急急急急急急,先谢谢各位大哥大姐们!!!
- 初来CSDN,有问题!请指教!菜鸟!!
- 虚心求教(java 体系)
- 有谁知道这个正则表达式这个特殊替换的??
- 关于抽象类的理解问题:
- 一个集合,怎么样才能把符合某一特征的数据取出来?
- 大家做GUI时有什么好用又免费的日期控件呢?
- 才开始学JAVA,几个学习方面的问题!
- eclipse平台下开发得新版velocity+webwork+hibernate例子程序。供初学者下载研究。(来者有分)
- 传递的是对象,但居然没有遵守引用传递的规则
2) 客户端 socket=server.accept(); 中得到getInputStream(),得到服务器返回值
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(),得到服务器返回值"
对于这句话我不知道您是什么意思?您是想让我在客户端加这句,还是说我写的这句不应该在服务器端,应该在客户端?