Java线程错误:线程中的异常错误加上数组索引越界异常加上套接字异常/连接异常
我正在创建两个程序文件(一个客户端一个服务器) 每个文件有一个线程(一个线程用于服务器,一个线程用于客户端) 在运行时,应该只有一台服务器,并且应该有多个和/或可能无限多个客户端同时连接到该服务器) 为了让多个客户端运行,用户打开多个命令提示符/mac终端窗口(每个窗口都是一个客户端)(一个窗口是服务器,因此至少需要两个窗口才能运行) 连接客户端后,它可以向服务器发送消息(utf-8字符串)。它还将从服务器接收从其他连接的客户端发送的所有消息(它不会接收从自身发送的消息) 线程/数组索引越界错误(eclipse)中异常的屏幕截图: 套接字异常错误(服务器)的屏幕截图: 客户端错误的屏幕截图: 服务器代码(ChatServer.java):Java线程错误:线程中的异常错误加上数组索引越界异常加上套接字异常/连接异常,java,multithreading,sockets,Java,Multithreading,Sockets,我正在创建两个程序文件(一个客户端一个服务器) 每个文件有一个线程(一个线程用于服务器,一个线程用于客户端) 在运行时,应该只有一台服务器,并且应该有多个和/或可能无限多个客户端同时连接到该服务器) 为了让多个客户端运行,用户打开多个命令提示符/mac终端窗口(每个窗口都是一个客户端)(一个窗口是服务器,因此至少需要两个窗口才能运行) 连接客户端后,它可以向服务器发送消息(utf-8字符串)。它还将从服务器接收从其他连接的客户端发送的所有消息(它不会接收从自身发送的消息) 线程/数组索引越界错误
import java.io.*;
导入java.net。*;
导入java.util.*;
导入静态java.nio.charset.StandardCharsets.*;
公共类聊天服务器
{
ChatServer ChatServer=新建ChatServer();
专用静态插座;
公共静态void main(字符串参数[])
{
线程ChatServer1=新线程()
{
公开作废运行()
{
System.out.println(“服务器线程正在运行”);
尝试
{
int端口号1=0;
int numberOfClients=0;
布尔保持修剪=真;
如果(参数长度>0)
{
端口号1=整数.valueOf(args[0]);
}
System.out.println(“等待端口上的连接”+端口号1);
尝试
{
ServerSocket ServerSocket=新的ServerSocket(端口号1);
socket=serverSocket.accept();
}
捕获(IOE异常)
{
e、 printStackTrace();
}
System.out.println(“侦听端口上的连接:”+(端口号1));
同时(继续修剪)
{
//创建客户端列表
ArrayList ListofClient=新的ArrayList();
//连接到客户端
//socket=serverSocket.accept();
//将新客户机添加到列表中,这是添加新客户机的正确方法吗?还是应该在for循环中?
客户名单。添加(“新客户”);
numberOfClients+=1;
System.out.println(“客户端已连接。正在等待消息…”);
添加(“新客户”+客户数量);
//从客户端读取编码的utf-8消息,从utf-8格式解码
字符串messagefromclientencodertf8=“”;
BufferedReader BufReader 1=新的BufferedReader(新的InputStreamReader(socket.getInputStream(),“UTF-8”);
字符串MessageFromClientDecodedFromUTF8=BufReader1.readLine();
byte[]bytes=messagefromclientencodertf8.getBytes(“UTF-8”);
String MessageFromClientDecodedUTF8=新字符串(字节,“UTF-8”);
//将消息转发给除它来自的客户端之外的所有其他客户端
对于(int i=0;i
ChatClient.java的代码:
import java.io.*;
import java.net.*;
import java.util.*;
import static java.nio.charset.StandardCharsets.*;
public class ChatClient
{
static int numberOfClients = 0;
public static void main(String args[])
{
ChatClient chatclient = new ChatClient();
//If I wanted to create multiple clients, would this code go here? OR should the new thread creation be outside the while(true) loop?
while (true)
{
String host = "localhost";
int numberOfClients = 0;
Thread ChatClient1 = new Thread ()
{
public void run()
{
try
{
//Client begins, gets port number, listens, connects, prints out messages from other clients
int port = 0;
int port_1number1 = 0;
int numberofmessages = 0;
String[] messagessentbyotherclients = null;
System.out.println("Try block begins..");
System.out.println("Chat client is running");
String port_number1= args[0];
System.out.println("Port number is: " + port_number1);
if(args.length>0)
{
port = Integer.valueOf(port_number1);
}
System.out.println("Listening for connections..");
System.out.println( "Listening on port: " + port_number1 );
boolean KeepRunning = true;
while(KeepRunning)
{
for(int i = 0; i < numberOfClients; i++)
{
System.out.println(messagessentbyotherclients);
}
try
{
Socket clientSocket = new Socket("localhost", port);
InetAddress inetlocalhost = InetAddress.getByName("localhost");
SocketAddress localhost = new InetSocketAddress(inetlocalhost, port);
clientSocket.connect(localhost, port);
System.out.println("Client has connected");
//client creates new message from standard input
OutputStream os = clientSocket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
}
catch (IOException e)
{
e.printStackTrace();
}
//creating message to send from standard input
String newmessage = "";
try
{
// input the message from standard input encoded in UTF-8 string format
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
String line = "";
System.out.println( "Standard input (press enter then control D when finished): " );
while( (line= input.readLine()) != null )
{
newmessage += line + " ";
input=null;
}
}
catch ( Exception e )
{
System.out.println( e.getMessage() );
}
//Sending the message to server
String sendMessage = newmessage;
try
{
Socket clientSocket = new Socket("localhost", port);
SocketAddress localhost = null;
clientSocket.connect(localhost, port);
OutputStream os = clientSocket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
bw.write(sendMessage + "\n");
bw.flush();
}
catch (IOException e)
{
e.printStackTrace();
}
System.out.println("Message sent to server: "+sendMessage);
}
}
finally
{
}
}
};
ChatClient1.start();
}
}
}
import java.io.*;
导入java.net。*;
导入java.util.*;
导入静态java.nio.charset.StandardCharsets.*;
公共类聊天客户端
{
静态int numberOfClients=0;
公共静态void main(字符串参数[])
{
ChatClient ChatClient=新ChatClient();
//如果我想创建多个客户机,那么这段代码会出现在这里吗?还是新线程的创建应该在while(true)循环之外?
while(true)
{
String host=“localhost”;
int numberOfClients=0;
线程ChatClient1=新线程()
{
公开募捐
{
import java.io.*;
import java.net.*;
import java.util.*;
import static java.nio.charset.StandardCharsets.*;
public class ChatClient
{
static int numberOfClients = 0;
public static void main(String args[])
{
ChatClient chatclient = new ChatClient();
//If I wanted to create multiple clients, would this code go here? OR should the new thread creation be outside the while(true) loop?
while (true)
{
String host = "localhost";
int numberOfClients = 0;
Thread ChatClient1 = new Thread ()
{
public void run()
{
try
{
//Client begins, gets port number, listens, connects, prints out messages from other clients
int port = 0;
int port_1number1 = 0;
int numberofmessages = 0;
String[] messagessentbyotherclients = null;
System.out.println("Try block begins..");
System.out.println("Chat client is running");
String port_number1= args[0];
System.out.println("Port number is: " + port_number1);
if(args.length>0)
{
port = Integer.valueOf(port_number1);
}
System.out.println("Listening for connections..");
System.out.println( "Listening on port: " + port_number1 );
boolean KeepRunning = true;
while(KeepRunning)
{
for(int i = 0; i < numberOfClients; i++)
{
System.out.println(messagessentbyotherclients);
}
try
{
Socket clientSocket = new Socket("localhost", port);
InetAddress inetlocalhost = InetAddress.getByName("localhost");
SocketAddress localhost = new InetSocketAddress(inetlocalhost, port);
clientSocket.connect(localhost, port);
System.out.println("Client has connected");
//client creates new message from standard input
OutputStream os = clientSocket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
}
catch (IOException e)
{
e.printStackTrace();
}
//creating message to send from standard input
String newmessage = "";
try
{
// input the message from standard input encoded in UTF-8 string format
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
String line = "";
System.out.println( "Standard input (press enter then control D when finished): " );
while( (line= input.readLine()) != null )
{
newmessage += line + " ";
input=null;
}
}
catch ( Exception e )
{
System.out.println( e.getMessage() );
}
//Sending the message to server
String sendMessage = newmessage;
try
{
Socket clientSocket = new Socket("localhost", port);
SocketAddress localhost = null;
clientSocket.connect(localhost, port);
OutputStream os = clientSocket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
bw.write(sendMessage + "\n");
bw.flush();
}
catch (IOException e)
{
e.printStackTrace();
}
System.out.println("Message sent to server: "+sendMessage);
}
}
finally
{
}
}
};
ChatClient1.start();
}
}
}