Skip to content

Instantly share code, notes, and snippets.

@AhmadVatani
Last active April 13, 2017 10:50
Show Gist options
  • Save AhmadVatani/1444e7c1ff9466e0beaf67f176497758 to your computer and use it in GitHub Desktop.
Save AhmadVatani/1444e7c1ff9466e0beaf67f176497758 to your computer and use it in GitHub Desktop.
Android socket connection example
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
public class SocketConnection {
private final String SERVER_IP = ""; //Add server ip
private final int SERVER_PORT = 0; //Add your server port here
private OnStatusChanged mStatusListener = null;
private OnMessageReceived mMessageListener = null;
private boolean isRunning = false;
private String retrieveMessage;
private PrintWriter printWriter;
private BufferedReader bufferedReader;
private Socket socket = null;
public static final int WAITING = 0;
public static final int CONNECTED = 1;
public static final int DISCONNECTED = 2;
public static final int ERROR = 3;
public interface OnStatusChanged {
void statusChanged(int status);
}
public interface OnMessageReceived {
void messageReceived(String message);
}
public SocketConnection(OnStatusChanged statusChangedListener, OnMessageReceived messageReceivedListener) {
mStatusListener = statusChangedListener;
mMessageListener = messageReceivedListener;
isRunning = true;
try {
InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
mStatusListener.statusChanged(WAITING);
socket = new Socket(serverAddr, SERVER_PORT);
try {
printWriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
mStatusListener.statusChanged(CONNECTED);
bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while (isRunning) {
retrieveMessage = bufferedReader.readLine();
if (retrieveMessage != null && mMessageListener != null) {
mMessageListener.messageReceived(retrieveMessage);
}
else {
mStatusListener.statusChanged(DISCONNECTED);
}
retrieveMessage = null;
}
} catch (Exception e) {
mStatusListener.statusChanged(ERROR);
} finally {
socket.close();
}
} catch (Exception e) {
mStatusListener.statusChanged(ERROR);
}
}
public void sendMessage(String message) {
if (printWriter != null && !printWriter.checkError()) {
printWriter.println(message);
printWriter.flush();
}
}
public void stop() {
isRunning = false;
try {
if (socket != null && !socket.isClosed()) {
socket.close(); // close socket
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
@AhmadVatani
Copy link
Author

If you want to set custom header, just should write them to printWriter .
When you write

writer.print ("GET " + szUrl + " HTTP/1.0\r\n\r\n");

The \r\n\r\n bit is sending a line-feed/carriage-return to end the line and then another one to indicate that there are no more headers. This is a standard in both HTTP and email formats, i.e. a blank line indicates the end of headers. In order to add additional headers you just need to not send that sequence until you're done. You can do the following instead

writer.print ("GET " + szUrl + " HTTP/1.0\r\n"); 
writer.print ("header1: value1\r\n"); 
writer.print ("header2: value2\r\n"); 
writer.print ("header3: value3\r\n"); 
// end the header section
writer.print ("\r\n"); 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment