This is the class in question
public class ServerLink {
private static Context context;
private Socket sock;
private String IP;
private int Port;
private InputStream is;
private OutputStream out;
private String temp;
private boolean bTemp;
public ServerLink(Context contxt){
this(contxt,"18.220.103.72",2267);
}
public ServerLink(Context contxt,String inIP, int inPort){
context = contxt;
IP = inIP;
Port = inPort;
}
public boolean sendLoginToServer(final String pass, final String username){
Thread t1 = new Thread(new Runnable() {
public void run()
{
try{
bTemp = false;
sock = new Socket(IP,Port);
out = sock.getOutputStream();
is = sock.getInputStream();
sendToServer("Login");
sendToServer(username.length() + ";" + username + ";" + pass);
temp = "";
String serverAnswer = Listener();
if(serverAnswer.equals("")){
Toast.makeText(context,"WTF",Toast.LENGTH_LONG).show();
}
else if(serverAnswer.equals("login")){
Toast.makeText(context,"Yay",Toast.LENGTH_LONG).show();
bTemp = true;
}
else if(serverAnswer.equals("nigol")){
Toast.makeText(context,"Nay",Toast.LENGTH_LONG).show();
}
}
catch (java.io.IOException e){
Toast.makeText(context,e.getMessage().toString(),Toast.LENGTH_LONG).show();
}
}});
t1.start();
return bTemp;
}
public void sendToServer(String input){
try{
String toSend = input;
byte[] toSendBytes = toSend.getBytes();
int toSendLen = toSendBytes.length;
byte[] toSendLenBytes = new byte[4];
toSendLenBytes[0] = (byte)(toSendLen & 0xff);
toSendLenBytes[1] = (byte)((toSendLen >> 8) & 0xff);
toSendLenBytes[2] = (byte)((toSendLen >> 16) & 0xff);
toSendLenBytes[3] = (byte)((toSendLen >> 24) & 0xff);
out.write(toSendLenBytes);
out.write(toSendBytes);
}
catch (java.io.IOException e){
Toast.makeText(context,e.getMessage().toString(),Toast.LENGTH_LONG).show();
}
}
public String Listener(){
try {
byte[] lenBytes = new byte[4];
is.read(lenBytes, 0, 4);
int len = (((lenBytes[3] & 0xff) << 24) | ((lenBytes[2] & 0xff) << 16) |
((lenBytes[1] & 0xff) << 8) | (lenBytes[0] & 0xff));
byte[] receivedBytes = new byte[len];
is.read(receivedBytes, 0, len);
String message = new String(receivedBytes, 0, len);
return message;
}
catch (java.io.IOException e){
Toast.makeText(context,e.getMessage().toString(),Toast.LENGTH_LONG).show();
return e.getMessage().toString();
}
}
}
It crashes my app when called. I tried using a new thread for each, and this code does work, I've tried using it as methods, but it is a group project so I want a class everyone can easily use, but doesn't work currently. It should connect to a server that returns login if successful, nigol if unsuccessful, the server does work, we've tested it with other platforms, but I cannot seem to get this class correct