I have a little issue sending from TCPClient class a message to TCPServer. This is my code :
TCPServer.java
package tcp1;
import java.net.*;
import java.io.*;
// marea diferenta e ca de fiecare data cand primeste date, se deschide un thread
import java.net.*;
import java.io.*;
public class TCPServer extends Thread
{
protected static boolean serverContinue = true;
protected Socket clientSocket;
// RECIEVER - has the propouse to wait and get data and send response after
public static void main(String[] args) throws IOException
{
ServerSocket serverSocket = null;
try
{
serverSocket = new ServerSocket(10008);
System.out.println("Connection Socket Created");
try
{
while (serverContinue)
{
serverSocket.setSoTimeout(10000);
System.out.println("Waiting for Connection");
try
{
new TCPServer(serverSocket.accept());
}
catch (SocketTimeoutException ste)
{
System.out.println("Timeout Occurred");
}
}
}
catch (IOException e)
{
System.err.println("Accept failed.");
System.exit(1);
}
}
catch (IOException e)
{
System.err.println("Could not listen on port: 10008.");
System.exit(1);
}
finally
{
try
{
System.out.println("Closing Server Connection Socket");
serverSocket.close();
}
catch (IOException e)
{
System.err.println("Could not close port: 10008.");
System.exit(1);
}
}
}
private TCPServer(Socket clientSoc)
{
clientSocket = clientSoc;
start();
}
public void run()
{
System.out.println("New Communication Thread Started");
try
{
System.out.println("11111");
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(),true);
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
// this dosent sent anything, unless i send something from the CLIENT to the SERVER
out.println("sent test ...");
System.out.println("222222");
String inputLine;
while ((inputLine = in.readLine()) != null)
{
System.out.println("RECV : " + inputLine);
if (inputLine.equals("?"))
{
inputLine = new String("\"Bye.\" ends Client, "+"\"End Server.\" ends Server");
}
System.out.println("SEND : A message was sent : " + inputLine);
out.println("MSG_OK ["+inputLine+"]");
// commenzi pe care i le poti da
if (inputLine.equals("Bye."))
{
break;
}
if (inputLine.equals("End Server."))
{
serverContinue = false;
}
}
out.close();
in.close();
clientSocket.close();
}
catch (IOException e)
{
System.err.println("Problem with Communication Server");
System.exit(1);
}
}
}
TCPClient.java
package tcp1;
import java.io.*;
import java.net.*;
// tcp client e la fel cu celalalt
public class TCPClient
{
// SENDER
public static void main(String[] args) throws IOException
{
// aici are localhost
String serverHostname = new String("127.0.0.1");
if (args.length > 0)
{
serverHostname = args[0];
}
System.out.println("Attemping to connect to host " +serverHostname + " on port 10008.");
// aici astea sunt null
Socket echoSocket = null;
PrintWriter out = null;
BufferedReader in = null;
// Si creaza un socket
try
{
echoSocket = new Socket(serverHostname, 10008);
out = new PrintWriter(echoSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
}
catch (UnknownHostException e)
{
System.err.println("Don't know about host: " + serverHostname);
System.exit(1);
}
catch (IOException e)
{
System.err.println("Couldn't get I/O for " + "the connection to: " + serverHostname);
System.exit(1);
}
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String userInput;
// Si aici pentru fiecare mesaj primit, zice ceva
System.out.println("Type Message (\"Bye.\" to quit)");
while ((userInput = stdIn.readLine()) != null)
{
System.out.println("SEND : "+userInput);
out.println(userInput);
// end loop
if(userInput.equals("Bye."))
{
break;
}
System.out.println("RECV : " + in.readLine());
}
out.close();
in.close();
stdIn.close();
echoSocket.close();
}
}
OUTPUT TCPServer.java
Connection Socket Created
Waiting for Connection
Waiting for Connection
New Communication Thread Started
11111
222222
RECV : qqqqqqqqqqqqqqqq
SEND : A message was sent : qqqqqqqqqqqqqqqq
Timeout Occurred
Waiting for Connection
RECV : send message
SEND : A message was sent : send message
RECV : hello
SEND : A message was sent : hello
Timeout Occurred
Waiting for Connection
^Croot@master345:~/dad/curs4/tcp2_multiple_users#
OUTPUT TCPClient.java
Attemping to connect to host 127.0.0.1 on port 10008.
Type Message ("Bye." to quit)
qqqqqqqqqqqqqqqq
SEND : qqqqqqqqqqqqqqqq
RECV : sent test ...
send message
SEND : send message
RECV : MSG_OK [qqqqqqqqqqqqqqqq]
hello
SEND : hello
RECV : MSG_OK [send message]
^Croot@master345:~/dad/curs4/tcp2_multiple_users#
I don't seem to get it why i can't sent data from TCPCLient to TCPServer. The main idea is that all Clients will be connected to the Server and Server will give commands to them and get back a response.
Only when i send a message from Client to Server i managed to send data from Server to Client. Also i have to say that the ip of the Client can be local or from a 3G modem, so i also need to solve this.
Any ideas?
Aucun commentaire:
Enregistrer un commentaire