我正在嘗試使用java套接字編寫聊天應用程序... 當服務器作爲用戶名和客戶端存儲時,服務器將以 作爲第一步存儲客戶端時,服務器偵聽端口以供客戶端使用鏈接套接字作爲sockettuple
(一個對象,其中有用戶名作爲字符串和鏈接插座作爲套接字) ..然後在存儲它發送客戶端作爲歡迎您連接..服務器偵聽listenthread
和 通信發生在單獨的線程..客戶端創建的套接字發生在主線程中並向服務器寫入 在單獨的線程中 在此存儲套接字元組發生錯誤 但發送消息f ROM服務器到客戶端Socketclosed exception
上 發生了服務器的寫入和客戶端的讀線程......Java Socket異常套接字關閉
// mainserver.java
import java.net.*;
import java.io.*;
class listenthread extends Thread{
ServerSocket mserver;
Socket link,targetsocket;
String user,targetuser;
sockettuple targettuple,starray[]= new sockettuple[10];
int i=0;
public listenthread(ServerSocket mserver){
try{
System.out.println("Inside Listenthread constructor");
this.mserver=mserver;
}
catch (Exception e){
System.out.println("Exception inside listenthread constructor"+e);
}
}
public void run(){
try{
while(true){
System.out.println("Listening.....");
link=mserver.accept();
System.out.println("Accepted");
BufferedReader intosockb = new BufferedReader(new InputStreamReader(link.getInputStream()));
user= intosockb.readLine();
System.out.println(user + "Accepted");
starray[i] =new sockettuple(link,user);
i++;
System.out.println(starray[i-1].username);
new servwrite(link).start();
intosockb.close();
}
}
catch(Exception e){
System.out.println("Exception inside listenthread run "+e);
e.printStackTrace();
}
}
}
class servwrite extends Thread{
Socket link;
public servwrite(Socket link){
this.link=link;
}
public void run(){
try{
PrintStream fromserv=new PrintStream(link.getOutputStream());
fromserv.print("You are connected...Enter the target user:");
}
catch(Exception e){
System.out.println("Exception inside run of serv write thread..."+e);
}
}
}
public class mainserver {
public static void main(String args[]){
try{
ServerSocket Servermain= new ServerSocket(4579);
System.out.println(" Server main Socket created");
new listenthread(Servermain).start();
}
catch(Exception e){
System.out.println("Exception inside main "+e);
}
}
}
//Clientmenu.java
import java.net.*;
import java.io.*;
class clientread extends Thread{
Socket client;
BufferedReader key=null;
public clientread(Socket client){
try{
this.client=client;
key= new BufferedReader(new InputStreamReader(client.getInputStream()));
}
catch(Exception e){
System.out.println("Exception inside client read thread's constructor");
e.printStackTrace();
}
}
public void run(){
try{
System.out.println("First line inside run of client read thread before cmsg readline");
String cmsg= key.readLine();
System.out.println(cmsg);
}
catch(Exception e){
System.out.println("Exception inside run of client read thread..."+e);
e.printStackTrace();
}
}
}
public class Clientmenu {
public static void main(String args[]){
String user=" ";
String targetuser="";
Socket newclient;
BufferedReader fromtheserv;
try{
newclient = new Socket("127.0.0.1",4579);
System.out.print("Enter username:");
BufferedReader key= new BufferedReader(new InputStreamReader(System.in));
user=key.readLine();
PrintStream fromclientp=new PrintStream(newclient.getOutputStream());
fromclientp.print(user);
new clientread(newclient).start();
fromclientp.close();
}
catch(Exception e){
System.out.println("Exception inside CLientmenu "+e);
}
}
}
傾銷整個代碼並要求我們解決它並不是在這裏提問的好方法。只發布相關的代碼。 –