Ok, I'm making a messenger server that can communicate with multiple clients at once.
Description of program ____________________________
I have a runnable class set up, with main spawning a new instance of the runnable for each newly accepted client. As a third class I have that will keep track of all the sockets currently in use. Each instance of the runnable class will have an instance of this class that will hold all the other sockets minus the native socket. The main class also has an instance of this class which holds all client sockets. After the server has accepted a client and has made a new instance runnable for it, that instance is then passed all of the previous client sockets, as well as being added to all previous client instances.
End ______________________________________________
import java.io.*;
import java.net.*;
public class ServerMessenger {
public static void main(String[] args) throws IOException {
ServerSocket ssocket;
ServerThread[] clientList;
clientList = new ServerThread[10];
int amnt = 0;
int i = 0;
int port = 7778;
ssocket = new ServerSocket(port);
while(true)
{
try
{
System.out.println("Waiting for client connection on port " + ssocket.getLocalPort() + "...");
Socket newClient = ssocket.accept();
System.out.println("Just connected to " + server.getRemoteSocketAddress());
clientList[amnt] = new ServerThread(newClient);
clientList[amnt].start();
while(i < amnt)
{
clientList[i].AddClient(newClient);
clientList[amnt].AddClient(clientList[i].GetSocket());
i++;
}
amnt++;
if(amnt > 9){
System.out.println("Server room is full!");
break;
}
i = 0;
}
catch(IOException e)
{
e.printStackTrace();
break;
}
}
}
}
I do not believe any of my problems are in the above class.
import java.net.*;
import java.io.*;
public class ServerThread implements Runnable{
private Socket client;
private ClientOut clientList = new ClientOut();
private Thread t;
ServerThread(Socket inclient) {
client = inclient;
}
public void ServerThread(){
}
public void run(){
String mssg;
try
{
while(true){
DataInputStream in = new DataInputStream(client.getInputStream());
mssg = client.getRemoteSocketAddress() + " -- Says: " + in.readUTF();
DataOutputStream outrec= new DataOutputStream(client.getOutputStream());
outrec.writeUTF("Message Received!");
outrec.flush();
System.out.println(mssg);
clientList.SendMssg(mssg);
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
public void start(){
t = new Thread(this);
t.start();
}
public void AddClient(Socket newclient){
clientList.AddClient(newclient, client);
}
public Socket GetSocket(){
return client;
}
}
import java.net.*;
import java.io.*;
public class ClientOut {
private Socket[] ClientList = new Socket[10];
private int amnt;
public void ClientOut(){
amnt = 0;
}
public void AddClient(Socket newClient, Socket curClient){
try{
ClientList[amnt] = newClient;
OutputStream toClient = curClient.getOutputStream();
DataOutputStream outtoclient = new DataOutputStream(toClient);
outtoclient.writeUTF(newClient.getRemoteSocketAddress() + " has joined the chat server!");
outtoclient.flush();
amnt ++;
}
catch(IOException e)
{
}
}
public void SendMssg(String mssg){
int i = 0;
while (i < amnt)
{
try
{
DataOutputStream out = new DataOutputStream(ClientList[i].getOutputStream());
out.writeUTF(mssg);
out.flush();
i++;
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
}
The compiler says all my issues are in the ServerThread class. In the run() method, the outrec.writeUTF("Message Received!"); is only ever sent on the first received message. I tried outrec.close(); after the flush, but then I had problems with the compiler telling me the socket was closed, even though I don't use the stream again without reestablishing it. Same with all other instance of DataOutput and DataInput. If I close them, the compiler complains. What am I doing wrong here?
Aucun commentaire:
Enregistrer un commentaire