这个好像没有public void run()方法啊!客户端创建实例化了10个客户端,每个客户端都向服务器发送请求,并接收服务器返回 的请求!
import java.io.*;
import java.net.*;
class SocketThreadClient extends Thread
{
public static int count = 0; public SocketThreadClient (InetAddress addr)
{
count++;
BufferedReader in = null;
PrintWriter out = null;
Socket sk = null;
try{
sk = new Socket (addr, 8000);
InputStreamReader isr;
isr = new InputStreamReader (sk.getInputStream ());
in = new BufferedReader (isr);
out = new PrintWriter (new BufferedWriter(new OutputStreamWriter(sk.getOutputStream ())), true);
System.out.println("count:"+count);
out.println ("Hello");
System.out.println (in.readLine ());
out.println ("BYE");
System.out.println (in.readLine ()); }
catch (IOException e)
{
System.out.println (e.toString ());
}
finally
{
out.println("END");
try
{
if (in != null)
in.close ();
if (out != null)
out.close ();
if (sk != null)
sk.close ();
}
catch (IOException e)
{
}
}
}
}public class SocketClient{
public static void main(String[] args) throws IOException,InterruptedException
{
InetAddress addr = InetAddress.getByName(null);
for(int i=0;i<10;i++)
new SocketThreadClient(addr);
Thread.currentThread().sleep(1000);
}
}
import java.io.*;
import java.net.*;
class SocketThreadClient extends Thread
{
public static int count = 0; public SocketThreadClient (InetAddress addr)
{
count++;
BufferedReader in = null;
PrintWriter out = null;
Socket sk = null;
try{
sk = new Socket (addr, 8000);
InputStreamReader isr;
isr = new InputStreamReader (sk.getInputStream ());
in = new BufferedReader (isr);
out = new PrintWriter (new BufferedWriter(new OutputStreamWriter(sk.getOutputStream ())), true);
System.out.println("count:"+count);
out.println ("Hello");
System.out.println (in.readLine ());
out.println ("BYE");
System.out.println (in.readLine ()); }
catch (IOException e)
{
System.out.println (e.toString ());
}
finally
{
out.println("END");
try
{
if (in != null)
in.close ();
if (out != null)
out.close ();
if (sk != null)
sk.close ();
}
catch (IOException e)
{
}
}
}
}public class SocketClient{
public static void main(String[] args) throws IOException,InterruptedException
{
InetAddress addr = InetAddress.getByName(null);
for(int i=0;i<10;i++)
new SocketThreadClient(addr);
Thread.currentThread().sleep(1000);
}
}
解决方案 »
- 如何忽略BufferedReader的readLine()读到的中间多个空格
- 该怎么看源代码才有效
- java程序怎么部署
- 编写java手机游戏的思路 大家看下对不对
- applet在XP IE怎么使用啊?
- 强制转换和调用toString()有什么区别???谢谢!!
- 关于Glass Pane问题,我如何使它看得见????
- 新手问题关于APPLET的!
- java.lang.ClassCastException:java.math.BigDecimal错误
- 招聘专职java编程人员
- 请问大家,JFrame能当作一个组件添加到TabbedPane中吗,
- 初学JAVA,Java applet和JSP有点混淆
implements Runnable时是必须重写run()方法的。