Hej,
försöker lösa en uppgift gällande socketkommunikation och har kört fast. Jag har laddat ner kod från nätet och skapat en server och en klient, och försöker sedan få en dator att kommunicera med en annan.
Servern startar snällt, och talar om att den väntar på connection requests från klienten. Klienten upprepar att servern har startats och att den väntar på req., men sen händer ingenting. Serverns andra try-sats, att den ska använda accept.() för klientens connection request, verkar man inte komma in i överhuvudtaget.
Hur kan jag ändra koden så att det funkar?
SERVERNS KOD:
package ServerUsingSockets;
import java.io.*;
import java.net.*;
public class Main
{
//Declaring variables
ServerSocket serverSocket = null;
Socket connectionSocket = null;
final int PORT_NUMBER = 3500;
PrintWriter printWriter = null;
BufferedReader bufferedReader = null;
String userInput;
public static void main(String args[]) throws IOException
{
Main serverUsingSockets = new Main();
serverUsingSockets.start();
}
private void start() throws IOException
{
try
{
//Listens here
serverSocket = new ServerSocket(PORT_NUMBER);
printMessage("Server started. Awaiting connection requests...");
}
catch(IOException ioexception)
{
//handle the exception
//maybe you want to inform the user that there is a problem and exit here?
printMessage("There is a problem starting the Server.");
printMessage("Exception: "+ioexception);
stopServer(1);
}
try
{
//Creates a socket for a client request
connectionSocket = serverSocket.accept();
printMessage("Server accepted connection from client.");
}
catch (IOException ioexception)
{
printMessage("Problem accepting connection from client");
}
printWriter = new PrintWriter(connectionSocket.getOutputStream(), true);
bufferedReader = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
//This is the input being captured from the client
while ((userInput = bufferedReader.readLine()) != null) {
printMessage("Received from client: "+userInput);
printWriter.println("Server responding with: "+userInput); //you can use your message to respond from server
if (userInput.equalsIgnoreCase("Exit")
|| userInput.equalsIgnoreCase("Bye")
|| userInput.equalsIgnoreCase("Quit"))
{
//breaking here. But you can retain until there is a user-intervention to stop the server
break;
}
}
printMessage("Server stopped.");
closeAllOpenStuff();
}
private void printMessage(String message)
{
System.out.println(message);
}
private void closeAllOpenStuff() throws IOException
{
printWriter.close();
bufferedReader.close();
connectionSocket.close();
serverSocket.close();
}
private void stopServer(int exitValue)
{
printMessage("Exiting Server process.");
System.exit(exitValue);
}
}
KLIENTENS KOD:
package serverusingsockets;
import java.io.*;
import java.net.*;
public class ClientUsingSockets
{
//Declaring variables
Socket connectionSocket = null;
PrintWriter printWriter = null;
BufferedReader bufferedReader = null ,clientInputReader = null;
final int PORT_NUMBER = 3500;
final String SERVER_NAME = "10.14.7.57";
String serverResponse;
public static void main(String args[]) throws IOException
{
ClientUsingSockets clientUsingSockets = new ClientUsingSockets();
clientUsingSockets.start();
}
private void start() throws IOException
{
try
{
//Connection to Server
connectionSocket = new Socket(SERVER_NAME, PORT_NUMBER);
printWriter = new PrintWriter(connectionSocket.getOutputStream(), true);
bufferedReader = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
printMessage("Connected to server.");
}catch(UnknownHostException unknownHostException) {
printMessage("Host " + SERVER_NAME + " unknown");
stopServer(1);
}catch(IOException ioexception) {
printMessage("Connection failed to host "+SERVER_NAME);
stopServer(1);
}
clientInputReader = new BufferedReader(new InputStreamReader(System.in));
String clientInput;
while ((clientInput = clientInputReader.readLine()) != null) {
//Sending the user input to the server
printWriter.println(clientInput);
if (clientInput.equalsIgnoreCase("Exit")
|| clientInput.equalsIgnoreCase("Bye")
|| clientInput.equalsIgnoreCase("Quit"))
break;
//Reading the response from the server
if ((serverResponse = bufferedReader.readLine()) != null)
{
printMessage(serverResponse);
}
}
closeAllOpenStuff();
}
private void printMessage(String message)
{
System.out.println(message);
}
private void closeAllOpenStuff() throws IOException
{
printWriter.close();
bufferedReader.close();
clientInputReader.close();
connectionSocket.close();
}
private void stopServer(int exitValue)
{
printMessage("Exiting Client process.");
System.exit(exitValue);
}
}