For some reasons i have to set up on android SocketServer which waits for connection with computer. All is going good, socket with client (computer) is creating, but streams don't open. It's just pausing without any error or message.
client:
s = new Socket(ip, 4567);
ois = new ObjectInputStream(s.getInputStream());
System.out.println("ois..");// not showing, so can't open input stream
oos = new ObjectOutputStream(s.getOutputStream());
System.out.println("oos.."); // same here
server:
socket = new ServerSocket(4567);
System.out.println("Waiting for connection,,"); // showing
client = socket.accept();
System.out.println("Connected"); //showing
ois = new ObjectInputStream(client.getInputStream());
System.out.println("ois.."); // not showing
oos = new ObjectOutputStream(client.getOutputStream());
System.out.println("oos.."); // not showing too
System.out.println("Stream,s opened");
My apk has INTERNET premissions. I'm using 4567 port. Any other application doesn't block the port.
What can be wrong?
Try opening the ObjectOutputStream first in your server.
socket = new ServerSocket(4567);
System.out.println("Waiting for connection,,"); // showing
client = socket.accept();
System.out.println("Connected"); //showing
oos = new ObjectOutputStream(client.getOutputStream());
ois = new ObjectInputStream(client.getInputStream());
System.out.println("Stream,s opened");
I don't see any timeouts in there, which is why it just stops.
There's probably some network problem; have you verified the IP address of the device is correct?
It's unlikely, but there could be some firewall rule blocking the connection.
Related
I'm using this code to constantly accept when a new Socket connect to Server:
while (true) {
Socket socket = null;
socket = ss.accept();
System.out.println("A client is connect...\n");
DataInputStream in = new DataInputStream(socket.getInputStream());
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
Thread acceptClient = new Thread(new ClientHandler(socket, in, out));
acceptClient.start();
}
But when a connection is created, I see this in the console:
A client is connect...
A client is connect...
A client is
connect...
I don't understand why this line is displayed 3 times. Can anyone explane?
I am trying to make raspberry listen to the java socket server. I run the server code with eclipse and then log in to raspberry desktop and run client.jar. When i run client.jar it does not connect to my server and does not throw any errors. It just 'stays' in the Lxterminal forever and does nothing.
Server
int port = 6666;
Inet4Address add = (Inet4Address) Inet4Address.getLocalHost();
System.err.println(add);
ServerSocket server = new ServerSocket(6666, 1, add);
Socket client = server.accept();
System.err.println("acc");
DataInputStream in = new DataInputStream(client.getInputStream());
DataOutputStream out = new DataOutputStream(client.getOutputStream());
while (true){
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
String line = read.readLine();
out.writeUTF(line);
out.flush();
System.err.println(in.readUTF());
}
Client
int port = 6666;
Socket server = new Socket("My ip", port);
DataInputStream in = new DataInputStream(server.getInputStream());
DataOutputStream out = new DataOutputStream(server.getOutputStream());
while (true)
{
String msg = in.readUTF();
if (msg.contentEquals("close"))
server.close();
else if (msg.equals("forward"))
{
out.writeUTF("I go forward master");
out.flush();
}
UPDATE:
I have resolved this problem few seconds ago.My firewall was blocking any connection so the raspberry couldn't connect.
Solution: Go to firewall and network connection and turn it off for private and public connections. I am using Win10
i'm working on an application to print to my local wifi printers but over 9100 port i have no response, over the 515 i have "connection refused".
the code is this:
Socket test = new Socket();
Socket sock = new Socket(ip,9100);
PrintWriter oStream = new PrintWriter(sock.getOutputStream());
oStream.printf("========================\n");
oStream.printf("PROVA STAMPA\n");
oStream.printf("HELLO SLOW SUD\n");
oStream.printf("========================"+"\n");
oStream.close();
sock.close();
and also i have try this:
Socket objSocket = new Socket();
InetSocketAddress objEndPoint = new InetSocketAddress(ip, 9100);
DataOutputStream objOutputStream;
objSocket.connect(objEndPoint, 6000);
objOutputStream = new DataOutputStream(objSocket.getOutputStream());
objOutputStream.write(("========================\nPROVA STAMPA\nHELLO SLOW SUD\n========================\n").getBytes());
objOutputStream.close();
objSocket.close();
Am getting set socket option failed exception in Android. What am I doing wrong ?
setsockopt failed: EBADF (Bad file number)
For the following code:
int timeout = 500;
Socket socket = new Socket(InetAddress.getByAddress(new byte[]{10, 0, 2, 2}),
Integer.parseInt(m.destPort));
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
oos.writeObject(m);
oos.close();
socket.setSoTimeout(timeOut);
ObjectInputStream iis = new ObjectInputStream(socket.getInputStream());
iis.readObject();
iis.close();
socket.close();
Closing the input or output stream of a socket closes the other stream and the socket.
Change
oos.close();
to
oos.flush();
(Poor coding in Android. It should throw a SocketException: socket closed.)
I am having trouble bouncing object between the client and the server.
Create an object. Update some fields. Send to Server. (this part works)
SomeObject thisObject = new SomeObject();
thisObject.setSomeValue(13); // update object to be sent
PrintStream toServer = new PrintStream(sock.getOutputStream());
ObjectOutputStream oos = new ObjectOutputStream(toServer);
oos.writeObject(thisObject);
oos.close();
toServer.println(oos); // send object to server
toServer.flush();
Right after this, server further updates some value and sets it to 1919;
ObjectInputStream objFromClient = new ObjectInputStream(new BufferedInputStream(
sock.getInputStream()));
Served thisObject = (Served) objFromClient.readObject();
thisObject.setSomeValue(1919);
Server then sends the object back to client
toClient = new PrintStream(sock.getOutputStream());
ObjectOutputStream oos = new ObjectOutputStream(toClient);
oos.writeObject(thisObject);
oos.close();
objFromClient.close();
sock.close();
But when the time comes to pick up the object back on the client side .. programs fails with Socket Closed exception
ObjectInputStream objFromServer = new ObjectInputStream(
new BufferedInputStream(sock.getInputStream())); //java.net.SocketException: Socket is closed
thisObject = (Served) objFromServer.readObject();
....
Please help me understand the issue
My guess is you're using the same Socket to both send and receive from the client. When you close the ObjectOutputStream on the client, this closes the underlying OutputStream, which closes sock. Then, when you try to reuse it below, it's been closed and throws an exception.
Instead, wait for the transaction to finish before closing your resources in the client code (which should be done in a finally block by the way). Or, if waiting is problematic, use a new Socket instead.