【发布时间】:2011-12-03 10:40:31
【问题描述】:
我正在尝试在 java 中进行一些套接字编程。我所拥有的是,我编写了这个程序来在我的本地机器上创建一个服务器和客户端,并为套接字输入和输出创建单独的两个线程放在 Se.java 文件中。类似地,文件 Cl.java
中客户端的套接字输入和输出的两个单独线程现在,当我编译它们时,它们都没有显示错误或异常,但是当我运行它时,出现一些异常的服务器继续运行,但客户端崩溃
我不知道哪里出了问题。我要做的就是为每个服务器和客户端创建两个线程,分别检查对套接字的读取和写入。任何建议都会很棒帮助
PS。对不起,如果问题太“noobie-ish”。请对我放轻松。这是我在这里的第一篇文章
这个相同的程序,在之前完美运行,当我在每个客户端文件 Cl.java 中只有一个线程来读取服务器输入并打印它,并在服务器端读取从客户端输入并打印。但是当我在每个服务器和客户端程序中创建两个线程来读取和写入字符串时,问题就来了。
任何建议都会受到欢迎。只是想在客户端和服务器端创建两个单独的线程用于读取和写入。
这是我的服务器完整代码:
Se.java
import java.net.*;
import java.io.*;
public class Se
{
public static int i=0;
public static BufferedReader stdIn =null;
public static void main (String [] args) throws IOException
{
ServerSocket serverSocket = null ;
try {
serverSocket = new ServerSocket (4321);
}
catch (IOException e)
{
System.err.println("Could not listen to the server port.");
System.exit (1);
}
Socket clientSocket= null;
try{
clientSocket= serverSocket.accept();
new Thread(new Sconsole(clientSocket)).start();
new Thread(new Slistener(clientSocket)).start();
}
catch(Exception e)
{
System.err.println("Accept failed");
System.exit(1);
}
stdIn.close();
clientSocket.close();
serverSocket.close();
}
}
/* thread to listen from client and print it at server console */
class Slistener implements Runnable
{
Socket channel=null;
public static PrintWriter out = null;
public static BufferedReader in = null;
public static String inputL=null;
public static boolean closed=true;
public Slistener(Socket Soc)
{
this.channel=Soc;
try
{
in=new BufferedReader(new InputStreamReader(channel.getInputStream()));
//closed=false;
new Thread(new Slistener(channel));
}
catch(IOException e)
{
System.out.println("error in making stream from server to client");
}
}
public void run ()
{
try
{
while((inputL=in.readLine())!= null)
{
System.out.println("SERVER:" + inputL);
if (inputL.equals ("Bye.")) break;
}
}
catch (IOException e)
{
System.out.println("Exception thrown at run loop.");
}
}
}
/* thread to listen from console at server end and print it at client console */
class Sconsole implements Runnable
{
Socket channel=null;
public static PrintWriter out = null;
public static BufferedReader in = null;
public static String inputL=null;
//public static boolean closed=true;
public Sconsole() {}
public Sconsole(Socket Soc)
{
this.channel=Soc;
try
{
out=new PrintWriter(channel.getOutputStream(), true);
in=new BufferedReader(new InputStreamReader(System.in));
//closed=false;
new Thread(new Sconsole(channel));
}
catch(Exception e)
{
System.out.println("error in writing to stream from server to client");
}
}
public void run ()
{
try
{
System.out.println("SERVER:");
while(true)
{
inputL=in.readLine();
out.println(inputL);
if (inputL.equals ("End.")) break;
}
//closed=true;
}
catch (Exception e)
{
System.out.println("Exception thrown at run loop.");
}
}
}
现在客户端代码:
Cl.java
import java.io.*;
import java.net.*;
public class Cl extends Thread
{
public static Socket channel= null;
public static void main(String args[]) throws IOException
{
try{
channel= new Socket ("localhost", 4321);
System.out.println("client connected");
new Thread(new Clistener(channel)).start();
new Thread(new Cconsole(channel)).start();
}
catch (Exception e)
{
System.err.println("Unknown Exception");
System.exit(1);
}
channel.close();
}
}
/* thread to listen from server and print it at client console */
class Clistener implements Runnable
{
Socket channel=null;
public static PrintWriter out = null;
public static BufferedReader in = null;
public static String inputL=null;
public Clistener() {}
public Clistener(Socket Soc)
{
this.channel=Soc;
try
{
in=new BufferedReader(new InputStreamReader(channel.getInputStream()));
new Thread(new Clistener(channel));
}
catch(IOException e)
{
System.out.println("error in making stream from server to client");
}
}
public void run ()
{
try
{
while(true)
{
inputL=in.readLine();
System.out.println("SERVER:" + inputL);
if (inputL.equals ("Bye.")) break;
}
}
catch (IOException e)
{
System.out.println("Exception thrown at run loop.");
}
}
}
/* thread to listen from client console and print it at server console */
class Cconsole implements Runnable
{
Socket channel=null;
public static PrintWriter out = null;
public static BufferedReader in = null;
public static String inputL=null;
public Cconsole(Socket Soc)
{
this.channel=Soc;
try
{
out=new PrintWriter(channel.getOutputStream(), true);
in=new BufferedReader(new InputStreamReader(System.in));
new Thread(new Cconsole(channel));
}
catch(Exception e)
{
System.out.println("error in making stream from server to client");
}
}
public void run ()
{
try
{
while((inputL=in.readLine())!= null )
{
System.out.println("CLIENT:");
out.println(inputL);
if (inputL.equals ("Bye.")) break;
}
}
catch (Exception e)
{
System.out.println("Exception thrown at run loop.");
}
}
}
由于每个程序中的两个线程都挂断了,它们分别有什么问题?欢迎任何建议:)
【问题讨论】:
-
你创建了一些你没有启动的线程(它们不会做任何事情)
-
刚刚编辑了它。现在分别调用它们并从 main 启动它们。仍然无法正常工作。
-
Clistener 正在从自身创建新的自身实例。我建议改用循环。
标签: java multithreading sockets networking