I'm new to android, java and socket programming so of course I'm trying to merge all three!
I'm creating a desktop based java server which will simply send a short string to an android app(client).
I have the android app running fine and it sends strings to the server which are read with no problems.
I have the server running and it recieves the strings with no problems.
The app(client) however has a socket timeout whenever I try to read the strings returned from the server. I'm using the same code so I can't understand the problem.
Anyway here's the code:
//SERVER//
import java.io.*;
import java.net.*;
import java.util.*;
public class GpsServer {
ServerSocket serversocket = null;
Socket socket = null;
public GpsServer()
{
try
{
serversocket = new ServerSocket(8189);
}
catch (UnknownHostException unhe)
{
System.out.println("UnknownHostException: " + unhe.getMessage());
}
catch (InterruptedIOException intioe)
{
System.out.println("Timeout while attempting to establish socket connection.");
} catch (IOException ioe)
{
System.out.println("IOException: " + ioe.getMessage());
}
}
public void refreshServer() {
try
{
socket = serversocket.accept();
InputStreamReader inputstreamreader = new InputStreamReader(socket.getInputStream());
BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
PrintWriter printwriter = new PrintWriter(socket.getOutputStream(),true);
System.out.println("socket read successful");
printwriter.println("Send Bye to disconnect.");
String lineread = "";
boolean done = false;
while (((lineread = bufferedreader.readLine()) != null) && (!done)){
System.out.println("Received from Client: " + lineread);
printwriter.println("You sent: " + lineread);
if (lineread.compareToIgnoreCase("Bye") == 0) done = true;
}
System.out.println("Closing connection");
socket.close();
bufferedreader.close();
inputstreamreader.close();
printwriter.close();
}
catch (UnknownHostException unhe)
{
System.out.println("UnknownHostException: " + unhe.getMessage());
}
catch (InterruptedIOException intioe)
{
System.out
.println("Timeout while attempting to establish socket connection.");
}
catch (IOException ioe)
{
System.out.println("IOException: " + ioe.getMessage());
}
}
public void nullify() {
try
{
socket.close();
serversocket.close();
}
catch (IOException ioe)
{
System.out.println("IOException: " + ioe.getMessage());
}
}
}
and the client...
//CLIENT
import java.io.*;
import java.net.*;
import java.util.*;
public class GpsClient {
public String lastMessage;
Socket socket = null;
String serverurl;
int serverport;
public GpsClient() {
lastMessage = "";
serverport = 8189;
serverurl = "192.168.10.4";
try
{
socket = new Socket(serverurl, serverport);
socket.setSoTimeout(10000);
}
catch (UnknownHostException unhe)
{
System.out.println("UnknownHostException: " + unhe.getMessage());
}
catch (InterruptedIOException intioe)
{
System.out.println("Timeout while attempting to establish socket connection.");
}
catch (IOException ioe)
{
System.out.println("IOException: " + ioe.getMessage());
}
}
public void retrieveNew() {
try {
socket = new Socket(serverurl, serverport);
socket.setSoTimeout(10000);
lastMessage = "connected!";
InputStreamReader inputstreamreader = new InputStreamReader(socket.getInputStream());
BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
PrintWriter printwriter = new PrintWriter(socket.getOutputStream(),true);
printwriter.println("Request");
lastMessage = "Request sent";
// Get error when I uncomment this block, i.e. try to read the response from the server
// "Timeout while attempting to establish socket connection."
// String lineread = "";
// while ((lineread = bufferedreader.readLine()) != null) {
// System.out.println("Received from Server: " + lineread);
// lastMessage = "Received from Server: " + lineread;
// }
lastMessage = "closing connection!";
bufferedreader.close();
inputstreamreader.close();
printwriter.close();
socket.close();
}
catch (UnknownHostException unhe)
{
System.out.println("UnknownHostException: " + unhe.getMessage());
}
catch (InterruptedIOException intioe)
{
System.out.println("Timeout while attempting to establish socket connection.");
}
catch (IOException ioe)
{
System.out.println("IOException: " + ioe.getMessage());
}
finally
{
try
{
socket.close();
}
catch (IOException ioe)
{
System.out.println("IOException: " + ioe.getMessage());
}
}
}
public void nullify() {
try
{
PrintWriter printwriter = new PrintWriter(socket.getOutputStream(),true);
printwriter.println("Bye");
printwriter.close();
socket.close();
}
catch (IOException ioe)
{
System.out.println("IOException: " + ioe.getMessage());
}
}
}
Thanks in advance!
Josh
Apart from all the issues noted in my comment, you are creating two sockets in the client, and your server is only written to process one connection. So it writes to the first one and tries to read from it, meanwhile your client is writing to the second one and trying to read from that.
When you fix that you will then hit a deadlock, as both sides are trying to read from each other.
Also you shouldn't use PrintWriter or PrintStream over the network, as they swallow exceptions you need to know about. Use BufferedWriter.
Try putting something at the end of the server message, like ETX or something like <end> or wrap the message in <msg>...</msg>.
Then on Android in the while loop check if you received it instead of checking (lineread = bufferedreader.readLine()) != null.
Related
I have used both a reader object and a scanner but while this client is connected to a simple socket server and they are both running, I cannot take an input from the user in the console and pass it to the server. pressing enter simply skips a line, scanner.nextLine() seems to capture nothing or something is going wrong when passing a variable to the output streamer.
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class Client {
public static void main(String[] args) {
Socket socket = null;
DataOutputStream outputStream = null;
BufferedReader reader = null;
Scanner scan = new Scanner(System.in);
String message;
String host = "macbook";
int port = 9999;
//attempts to connect to given host and port
try {
socket = new Socket(host, port);
outputStream = new DataOutputStream(socket.getOutputStream());
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: " + host +".");
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to: " + host+".");
e.printStackTrace();
}
// if everything has been initialized then write some data
if (socket != null && outputStream != null && reader != null) {
try {
message=scan.nextLine();
outputStream.writeBytes(message);
String responseLine;
while ((responseLine = reader.readLine()) != null) {
System.out.println("Server: " + responseLine);
if (responseLine.indexOf("Ok") != -1) {
break;
}
}
//closes client once communication with server has ended
outputStream.close();
reader.close();
socket.close();
} catch (UnknownHostException e) {
System.err.println("Trying to connect to unknown host: " + e);
} catch (IOException e) {
System.err.println("IOException: " + e);
}
}
}
}
I'm working on a little game that sends location data between a client an server to learn how Sockets work.
The server can send and receive data no problem, and the client can send data, but when the client tries to read in data from the server, the program hangs. (This part is commented out)
Server Code:
public void run() {
try {
serverSocket = new ServerSocket(10007);
} catch (IOException e) {
System.err.println("Could not listen on port: 10007.");
System.exit(1);
}
try {
System.out.println("Waiting for connection...");
clientSocket = serverSocket.accept();
} catch (IOException e) {
System.err.println("Accept failed.");
System.exit(1);
}
System.out.println("Connection successful");
System.out.println("Waiting for input.....");
while (true) {
try {
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
out = new PrintWriter(clientSocket.getOutputStream(), true);
if (in.readLine() != "0" && in.readLine() != null) {
setXY(in.readLine());
}
} catch (IOException e) {
e.printStackTrace();
}
out.println("X" + Graphics.charX);
out.println("Y" + Graphics.charY);
}
Client Code:
public void run() {
try {
System.out.println("Attemping to connect to host " + serverHostname + " on port " + serverPort + ".");
echoSocket = new Socket(serverHostname, serverPort);
} 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);
}
while (true) {
try {
in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
out = new PrintWriter(echoSocket.getOutputStream(), true);
/*if (in.readLine() != "0" && in.readLine() != null) {
setXY(in.readLine());
}*/
} catch (IOException e2) {
e2.printStackTrace();
}
out.println("X" + Graphics.charX);
out.println("Y" + Graphics.charY);
}
}
Any help is much appreciated!
You need two threads to read/write blocking sockets at the same time (which is what you're trying to do). When you call in.readLine(), the current thread will block until it receives a line of data.
This may be a stupid question, but here goes.
Im writing this chat program, where there is a server, and clients that can connect to it. I want to implement private messaging into the program, but I don't know how to get the clients to directly connect to eachother. For the server, I used a ServerSocket, which runs on a single port. To get that to work, I needed to forward a port to the server. Is there a way to get the clients to wait for connections, without forwarding a port to them?
Thanks
The whole point of TCP/IP is that a single client connects to a predefined port on a server. So yes, you'll also need to have a ServerSocket on the client that's going to accept the direct connection. You'll almost always run into trouble with port forwarding and the like, which is why UPnP was invented one day.
What you are trying to do is 'peer to peer' connectivity, aka P2P, which is always, by its very definition, plagued by firewalling problems. As such it's usually, especially for a chat, easier to use the central server as 'switchboard' server and relay the private messages as well.
I've written not long time ago a template for multiple client - server application, that might help you to solve your problem. The rest of your question was already answerd by #Niels, I think ;)
import java.net.*;
import java.io.*;
class ServeConnection extends Thread {
private Socket socket = null;
private BufferedReader in = null;
private PrintWriter out = null;
public ServeConnection(Socket s) throws IOException {
// init connection with client
socket = s;
try {
in = new BufferedReader(new InputStreamReader(
this.socket.getInputStream()));
out = new PrintWriter(this.socket.getOutputStream(), true);
} catch (IOException e) {
System.err.println("Couldn't get I/O.");
System.exit(1);
}
start();
}
public void run() {
System.out.println("client accepted from: " + socket.getInetAddress()
+ ":" + socket.getPort());
// get commands from client, until is he communicating or until no error
// occurs
String inputLine, outputLine;
try {
while ((inputLine = in.readLine()) != null) {
System.out.println("request: " + inputLine);
outputLine = inputLine;
out.println("I've recived "+outputLine);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("server ending");
out.close();
try {
in.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class Server {
public static void svr_main(int port) throws IOException {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(port);
} catch (IOException e) {
System.err.println("Could not listen on port: " + port);
System.exit(1);
}
System.out.println("Server ready");
try {
while (true) {
Socket socket = serverSocket.accept();
try {
new ServeConnection(socket);
} catch (IOException e) {
System.err.println("IO Exception");
}
}
} finally {
serverSocket.close();
}
}
}
class Client {
static Socket echoSocket = null;
static PrintWriter out = null;
static BufferedReader in = null;
public static void cli_main(int port, String servername) throws
IOException {
try {
echoSocket = new Socket(servername, port);
out = new PrintWriter(echoSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(
echoSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: " + servername);
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for " + servername);
System.exit(1);
}
System.out.println("Client ready!");
while (true) {
inputLine = (in.readLine().toString());
if (inputLine == null) {
System.out.println("Client closing!");
break;
}
// get the input and tokenize it
String[] tokens = inputLine.split(" ");
}
out.close();
in.close();
echoSocket.close();
System.out.println("Client closing");
}
}
public class MyClientServerSnippet{
public static void main(String[] args) throws IOException {
if (args.length == 0) {
System.err.println("Client: java snippet.MyClientServerSnippet<hostname> <port>");
System.err.println("Server: java snippet.MyClientServerSnippet<port>");
System.exit(1);
}
else if (args.length > 1) {
System.out.println("Starting client...\n");
Client client = new Client();
client.cli_main(3049, "127.0.0.1");
} else {
System.out.println("Starting server...\n");
Server server = new Server();
server.svr_main(3049);
}
}
}
I am new to java TCP socket. I tried to implement a server and a client. So the server should check input (do something) and send string to client. The client should send string to the server and look for an input string from the server (and do something). Both should loop checking and sending all the time if something new is available.
The client can send data to the server, the server receives it an can display/process this data.
But the data from the server isn't displayed by the client. Can someone tell me why the client isn't receiving the string from the server? Any better ideas to do endless loop? There will be only one client and one server.
while true:
server out------> send String-----> in client
in<----- sent String <------ out
this is the simplified server part:
public class MainActivity extends Activity {
Socket client;
ServerSocket server;
int serverport = 54321;
String inputData = null;
BufferedReader in;
PrintWriter out;
String outputData;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread(setupConnection).start();
}
private Runnable setupConnection = new Thread() {
public void run() {
try {
server = new ServerSocket(serverport);
while (true) {
client = server.accept();
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
out = new PrintWriter(client.getOutputStream(), true);
inputData = in.readLine();
InputStream inStream = new ByteArrayInputStream(inputData.getBytes());
in.close();
if (inputData != null) {
System.out.println(TAG + "-----Incoming Message---- " + inputData);
//this is working String is shown
} }
out.write("nothing to do?");
out.flush();
out.close();
}
} catch (SocketException e) {
Log.v(TAG, "SocketException: " + e);
e.printStackTrace();
} catch (IOException e) {
Log.v(TAG, "IOException: " + e);
e.printStackTrace();
}
}
the simplified client looks like this:
public class testClass {
public static void main(String[] args) throws IOException, InterruptedException {
Socket socket = null;
String host = "127.0.0.1";
int port = 54321;
PrintWriter out = null;
BufferedReader in = null;
while (true) {
try {
socket = new Socket(host, port);
out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (UnknownHostException e) {
System.out.println(TAG + "Error: " + e);
System.err.println("Don't know about host: localhost.");
System.exit(1);
} catch (IOException e) {
System.out.println(TAG + "Error: " + e);
System.err.println("Couldn't get I/O for " + "the connection to: localhost.");
System.exit(1);
}
out.println("Hello, is it me you're looking for...");
out.flush();
String input = in.readLine();
System.out.println("Input: " + input);
in.close();
out.close();
}
}
}
If readLine() returns null,the peer has closed the connection, and you must do likewise. And stop reading.
if you want implement this code in android , you faces many problems:
you can find the solution in this link:
Post JSON in android
in the following code may be fix this problem:
HttpPost post = new HttpPost("http://xxxxxx");
DefaultHttpClient httpClient = new DefaultHttpClient();
post.setEntity(new ByteArrayEntity(json.toString().getBytes()));
HttpResponse response = httpClient.execute(post);
return EntityUtils.toString(response.getEntity());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
System.out.println(">>>>>>>" + e.getMessage());
} catch (ClientProtocolException e) {
e.printStackTrace();
System.out.println(">>>>>>>" + e.getMessage());
} catch (IOException e) {
e.printStackTrace();
System.out.println(">>>>>>>" + e.getMessage());
}
I am trying to write a small socket program with client side in groovy and the server side in Java. Below is the code I wrote
client:
def s = new Socket("localhost", 4444);
s << "Server before withStreams\n";
s.withStreams { input, output ->
println"Sending message1"
output << "server message1\n"
}
s.close();
server:
import java.io.*;
import java.net.*;
public class Logger{
ServerSocket providerSocket;
Socket connection = null;
BufferedReader in;
String message="InitialMessage";
Logger(){}
void run()
{
try{
providerSocket = new ServerSocket(4444, 10);
try{
Thread.sleep(1000);
}
catch(InterruptedException ie)
{
System.out.println("Sleep Interrupted");
}
System.out.println("Waiting for connection");
connection = providerSocket.accept();
System.out.println("Connection received from " + connection.getInetAddress().getHostName());
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
do{
if(in.ready())
{
try{
System.out.println(in.read());
message = in.readLine();
System.out.println("client>" + message);
}
catch(IOException e)
{
System.out.println(e);
e.printStackTrace();
break;
}
}
} while(!message.equals("bye"));
}
catch(IOException ioException){
ioException.printStackTrace();
}
finally{
//4: Closing connection
try{
in.close();
providerSocket.close();
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
}
public static void main(String args[])
{
Logger server = new Logger();
while(true){
server.run();
}
}
}
When I execute both programs, Socket communication is established. But I get a IOException in server code when it reads from the socket (message = in.readLine();)
I guess there is some format problem in writing into socket in client. But not able to figure out the exact problem. Can anybody help?
You generally don't want to close your ServetSocket for each client connection. You want to do this once (or every time you start the server) then on each accept() handle the client connection and close the socket for that connection but keep the ServerSocket open until you want to stop the server.
Here's a rewritten version of your example server that also creates a new Thread for each client request to handle multiple concurrent requests. Note that since the test client doesn't send the terminating string "bye" the connection and socket stays open.
import java.io.*;
import java.net.*;
public class Logger {
private ServerSocket providerSocket;
Logger() {
}
public void start() {
try {
providerSocket = new ServerSocket(4444, 10);
while (true) {
System.out.println("Waiting for connection");
Socket connection = providerSocket.accept();
new Thread(new Job(connection)).start();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (providerSocket != null) {
System.out.println("Stopping server");
try {
providerSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
static class Job implements Runnable {
final Socket connection;
private static int id;
private int clientId = ++id;
public Job(Socket connection) {
this.connection = connection;
}
public void run() {
BufferedReader in = null;
try {
System.out.println("Connection " + clientId + " received from " + connection.getInetAddress().getHostName());
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String message = "InitialMessage";
do {
if (in.ready()) {
try {
// not sure why want to read one character then read the line
//int ch = in.read();
//System.out.println(ch);
// -1 if the end of the stream has been reached
//if (ch == -1) break;
message = in.readLine();
// null if the end of the stream has been reached
if (message == null) break;
System.out.println("client>" + message);
} catch (IOException e) {
System.out.println(e);
e.printStackTrace();
break;
}
}
} while (!message.equals("bye"));
} catch (IOException e) {
e.printStackTrace();
} finally {
//4: Closing connection
System.out.println("Close connection " + clientId);
if (in != null)
try {
in.close();
} catch (IOException ioException) {
ioException.printStackTrace();
}
try {
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void main(String args[]) {
Logger server = new Logger();
server.start();
}
}