I'm using a method called registerDevice() in my android app
code to send and receive specific data which contains multiple lines. But I keep getting this error:
W/System.err: java.net.SocketException: Connection reset
public void registerDevice(){
final Handler handler = new Handler();
Thread thread = new Thread(new Runnable() {
#Override
public void run() {
try {
Socket s = new Socket(gateway, 1234);
OutputStream out = s.getOutputStream();
PrintWriter output = new PrintWriter(out);
BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
output.println("hello world\r\n");
output.flush();
StringBuffer stringBuffer = new StringBuffer("");
String line = null;
while ((line = input.readLine()) != null) {
stringBuffer.append(line);
}
final String st = line.substring(5);
handler.post(new Runnable() {
#Override
public void run() {
if (st.trim().length() != 0)
Toast.makeText(getApplicationContext(),st,Toast.LENGTH_LONG).show();
}
});
output.close();
out.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
thread.start();
}
UPDATE: I changed the code to this:
Socket s = new Socket(gateway, 1234);
OutputStream out = s.getOutputStream();
PrintWriter output = new PrintWriter(out);
BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
output.println("hello world\r\n");
output.flush();
final String st = input.readLine();
handler.post(new Runnable() {
#Override
public void run() {
if (st.trim().length() != 0)
Toast.makeText(getApplicationContext(),"st",Toast.LENGTH_LONG).show();
}
});
output.close();
out.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
I'm Still getting exceptions on the "final String st = input.readLine();" line. I am supposed to get a MAC address from the server and then the server closes the connection.I checked the server and there is nothing wrong with it, it closes the connection AFTER it sends me the MAC.
Guys I found the problem, it was the packet I was sending, I just changed outout.println() to output.write(), so now the server recognized my command and sent the data I needed which led to my input stream not being empty and avoiding exceptions.
Related
I'm trying to add a client socket in the file ViewRootImpl.java. I'm creating the socket in a new thread with an handler because I need to comunicate between threads. I'm sending a message to Vthread every time performTraversal is called.
Client code in ViewRootImpl.java:
public class Vthread extends Thread{
Viewhandler mViewhandler;
Handler mhandler;
Socket client;
BufferedReader in;
PrintWriter out;
String s;
String line;
Vthread(Viewhandler handler){
mViewhandler = handler;
in = null;
out = null;
s = "hello";
client = null;
}
#Override
public void run(){
Looper.prepare();
try{
client = new Socket("10.0.2.2", 60000);
out = new PrintWriter(client.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
}catch(IOException e){
e.printStackTrace();
}
mhandler = new Handler(){
#Override
public void handleMessage(Message msg) {
try{
if(out != null && in != null && client != null){
out.println(s);
out.flush();
line = in.readLine();
}
}catch (IOException e) {
e.printStackTrace();
}
}};
Looper.loop();
}
}
The server code in the host:
Socket socket;
ServerSocket server;
SocketAddress sockaddr;
BufferedReader in = null;
PrintWriter out = null;
String line;
String s = "bye";
server = null;
try{
sockaddr = new InetSocketAddress("127.0.0.1", 60000);
server = new ServerSocket();
server.bind(sockaddr);
socket = server.accept();
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
}catch (IOException e) {
e.printStackTrace();
}
try{
System.out.println("connected");
while(!Thread.currentThread().isInterrupted()){
line = in.readLine();
System.out.println(line);
out.println(s);
}
if (server != null ) server.close();
}catch (IOException e) {
e.printStackTrace();
}
The connection is accepted but the server doesn't receive any message from client. A problem that I identified is that more than one process might be using the socket. So I used a file in the internal storage of my application to restric the socket to my application only, but the problem remains. Code to restrict the socket to my application:
if(mVthread.mhandler != null) {
try{
if(reader == null) reader = new BufferedReader(new FileReader(file));
Message msg = Message.obtain();
msg.arg1 = 1000;
mVthread.mhandler.sendMessage(msg);
}catch(Exception e){
e.printStackTrace();
}
}
EDIT
The problem is that the client socket sends a message but the server doesn't receive it. Both sides are blocked in the receive function. Any idea on what I am doing wrong?
I'm delving into sockets for the first time.
The point of the project is for the client to be able to get access to a contact list (CSV) in the server by writing "getall" and exit the program through just that command ("Exit").
The problem is that the client can only write the command and receive the list once and then the server doesn't respond to the client's input anymore.
Here is the socket code for the server and client respectively:
Server:
public class CatalogueServer extends CatalogueLoader {
ServerSocket serverSocket;
ArrayList<CatalogueEntry> catalogue;
public void startServer(int port, String catalogueFile) {
catalogue = loadLocalCatalogue(catalogueFile);
try {
serverSocket = new ServerSocket(port);
while (true) {
Socket clientSocket = serverSocket.accept();
new Thread(
new Runnable() {
public void run() {
try {
InputStream inputStream = clientSocket.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
BufferedReader BR = new BufferedReader(inputStreamReader);
OutputStream outputStream = clientSocket.getOutputStream();
PrintWriter PW = new PrintWriter(outputStream);
String clientInput;
while ((clientInput = BR.readLine()) != null) {
System.out.println(clientInput);
if (clientInput.equals("getall")) {
System.out.println(printCatalogue(catalogue));
PW.println(printCatalogue(catalogue));
PW.flush();
break;
} else if (clientInput.equals("exit")) {
clientSocket.close();
BR.close();
PW.close();
break;
} else {
PW.flush();
break;
}
}
PW.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
).start();
}
} catch (Exception i) {
i.printStackTrace();
}
}
}
Client:
public class TestClient {
public static void main(String[] args) {
try {
Socket socket = new Socket("localhost", 5253);
OutputStream outputStream = socket.getOutputStream();
InputStream inputStream = socket.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader BR = new BufferedReader(inputStreamReader);
while (true) {
String clientInput;
String serverFeedback;
PrintWriter PW = new PrintWriter(outputStream);
Scanner inputScan = new Scanner(System.in, "UTF-8");
clientInput = inputScan.nextLine();
PW.println(clientInput);
PW.flush();
while ((serverFeedback = BR.readLine()) != null) {
System.out.println(serverFeedback);
}
if (clientInput.equals("exit")) {
PW.close();
socket.close();
break;
}
PW.close();
}
}
catch (IOException e){
e.printStackTrace();
}
}
}
I have tried alternating the position and renewal of the readers and writers. But I'm uncertain of where exactly the problem starts.
When you do
while ((serverFeedback = BR.readLine()) != null) {
System.out.println(serverFeedback);
}
You are reading until you reach the end of the stream, i.e. until there is nothing left. As such there is nothing after this.
If you want to reuse the connection, you have to write the code which doesn't use this pattern and only reads until it should stop reading.
I have a problem with multithreading. I don't know why, but threads run in sequence.
There is a client-server application. I need to run several parallel threads for message exchange. The whole classes are too big, so I will show main parts.
Code of Client(This code runs every time when I push the button):
public ArrayList<Action> call() {
Thread myThready = new Thread(new Runnable()
{
public void run()
{
BufferedReader in;
PrintWriter out;
try{
Socket fromserver = new Socket(ip, PortID);
in = new BufferedReader(new InputStreamReader(fromserver.getInputStream()));
out = new PrintWriter(fromserver.getOutputStream(),true);
writeLog(query, ID, 0);
out.println(query+"ID"+ID);
String fserver = in.readLine();
writeLog(fserver, ID, 1);
out.println("exit");
out.close();
in.close();
fromserver.close();
}
catch (IOException io){
return;
}
}
});
myThready.start();
}
Code of Server:
public void run(){
flag=true;
System.out.println("Welcome to Server side!");
createLog();
ExecutorService service = Executors.newCachedThreadPool();
ServerSocket servers = null;
int n=4600;
try{
servers = new ServerSocket(n);
} catch( Exception e){
}
Socket fromclient = null;
while(true){
try {
System.out.print("Waiting for a client...");
fromclient = servers.accept();
System.out.println("Client connected.");
Callable<ArrayList<Action>> callable = new HandleThread(fromclient);
Future<ArrayList<Action>> future = service.submit(callable);
list.addAll(future.get());
} catch (Exception e) {
System.out.println("Can't accept.");
System.exit(-1);
}
}
}
This code does "accept()" and then creates new thread for some calculations.
Code of HandleThread:
public ArrayList<Action> call() {
BufferedReader in = null;
PrintWriter out= null;
try {
in = new BufferedReader(new
InputStreamReader(fromclient.getInputStream()));
out = new PrintWriter(fromclient.getOutputStream(),true);
String input,output;
System.out.println("Wait for messages.");
while ((input = in.readLine()) != null) {
//close filewriter thread if input==exit
if(input.equalsIgnoreCase("exit")){
break;
}
System.out.println(input);
String[] arr = input.split("ID");
System.out.println("+"+arr[0]);
ID = Integer.parseInt(arr[1]);
writeLog(arr[0], ID, 0);
process(arr[0], ID);
out.println(arr[0]);
writeLog(arr[0], ID, 1);
}
out.close();
in.close();
fromclient.close();
} catch(IOException e){
return null;
}
return list;
}
I don't know why this doesn't work. I have logs and I see that one thread runs only after another one. Not at the same time!
Please, help me!
Future#get() is a blocking call.
list.addAll(future.get());
The calling thread will wait until the task is done. As such, your server thread which calls accept() waits for each task to finish before it gets to the next one.
I have 2 programs: a client and a server.
The server creates a ServerSocket and the client connects using:
address = InetAddress.getByName(host);
conn = new Socket(address, port);
this works, but here is the problem: void mousePressed() { gets called once the mouse is clicked, executing this: (client side)
void mousePressed() {
try {
BufferedOutputStream os = new BufferedOutputStream(conn.getOutputStream());
OutputStreamWriter osw = new OutputStreamWriter(os, "US-ASCII");
osw.write("123");
osw.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
the server should receive the input using:
BufferedReader reader = new BufferedReader(
new InputStreamReader(new BufferedInputStream(conn.getInputStream())));
StringBuilder result = new StringBuilder();
for (String line = null; (line = reader.readLine()) != null;) {
result.append(line);
}
reader.close();
println(result.toString());
The server only receives the input after the socket has been closed with: conn.close(); on the client side or quitting the client. As i want to be able to click the mouse multiple times, i can't close the socket.
What can i do to send input without closing the socket?
Edit: connection code:
Server:
// init
ServerSocket socket1;
int main_port = 5204;
// in main
try {
socket1 = new ServerSocket(main_port);
Socket conn = socket1.accept();
} catch (Exception e) {
e.printStackTrace();
}
Client:
// init
String host = "localhost";
int port = 5204;
Socket conn;
InetAddress address;
// in main
try {
address = InetAddress.getByName(host);
conn = new Socket(address, port);
} catch (Exception e) {
e.printStackTrace();
}
My solution (based on other answers and comments):
1) Changing osw.write("123"); to osw.write("123\n"); in the client.
2) Replacing
BufferedReader reader = new BufferedReader(new InputStreamReader(new
BufferedInputStream(thread_cnn.getInputStream())));
StringBuilder result = new StringBuilder();
for (String line = null; (line = reader.readLine()) != null;) {
result.append(line);
}
println(result);
reader.close();
with
BufferedReader reader = new BufferedReader(new InputStreamReader(new BufferedInputStream(conn.getInputStream())));
String result = reader.readLine().toString();
println(result);
reader = null;
result = null;
on the server.
You are writing an incomplete line, and trying to read complete lines. Terminate the text you send with a line break so it can be read when it arrives.
Also, do not catch and ignore exceptions. If something goes wrong you will want to know about it.
try {
BufferedOutputStream os = new BufferedOutputStream(conn.getOutputStream());
OutputStreamWriter osw = new OutputStreamWriter(os, "US-ASCII");
osw.write("123\n");
osw.flush();
} catch (Exception e) {
e.printStackTrace();
}
I am trying to write a small program, that opens a server, creates a client that connects to this server and receives a message from it.
This is the Code so far
public static void main(String[] args) {
final ServerSocket serverSocket;
try {
serverSocket = new ServerSocket(12345);
Thread t = new Thread(){
public void run(){
try {
Socket server = serverSocket.accept();
PrintWriter writer = new PrintWriter(server.getOutputStream(), true);
writer.write("Hello World");
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
};
t.start();
Socket client = new Socket("localhost", 12345);
BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
String message = reader.readLine();
System.out.println("Received " + message);
} catch (IOException e1) {
e1.printStackTrace();
}
}
If i run program it keeps waiting in readLine() - so obviously the client does not receive the message from the server.
Has anyone got an idea why this isn' working?
Your reading thread is waiting for a newline in the data stream. Just change the server to use:
writer.write("Hello World\r\n");
and you'll get the result you were expecting. Alternatively, you can just close the server socket, and then readLine will return when it reaches the end of the data stream.
You should put the readline in a loop as follows:
public static void main(String[] args) {
final ServerSocket serverSocket;
try {
serverSocket = new ServerSocket(12345);
Thread t = new Thread() {
public void run() {
try {
Socket server = serverSocket.accept();
PrintWriter writer = new PrintWriter(server.getOutputStream(), true);
writer.write("Hello World");
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
};
t.start();
Socket client = new Socket("localhost", 12345);
BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
// Check this --------------------------------------------------->
String message = null;
while ((message = in.readLine()) != null) {
System.out.println("Received " + message);
break; //This break will exit the loop when the first message is sent by the server
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
You can read this documentation for further explanation: http://download.oracle.com/javase/tutorial/networking/sockets/