LamarSt LamarSt - 1 month ago 11
Git Question

Server(C) on the PI doesn't make connection with Client(Java)

I have 1 client written in Java and 1 server on the PI written in C making connection through a socket.

At first the server and the client could talk to each other. After a git merge it didn't work anymore but nothing has changed. Can someone see what's wrong?

Any insight would be greatly appreciated.

Thanks.

Udpclient:

package com.example.jobush50.test2;

import android.os.Message;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

public class Udpclient extends Thread {


String ipaddress;
int port;

DatagramSocket socket;
InetAddress address;

String msg;

public Udpclient(String addr, int prt) {
ipaddress = addr;
port = prt;
}

public void message(String message) {
msg = message;
}

public void run() {

try {

socket = new DatagramSocket();
address = InetAddress.getByName(ipaddress);

// send request
byte[] buf;
buf = msg.getBytes();

DatagramPacket packet =
new DatagramPacket(buf, buf.length, address, port);
socket.send(packet);

} catch (SocketException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (socket != null) {
socket.close();
}
}
}
}


MainActivity - Where socket is made:

package com.example.jobush50.test2;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

EditText editTextAddress, editTextPort;
Button buttonConnect;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

editTextAddress = (EditText) findViewById(R.id.editText_ip);
editTextPort = (EditText) findViewById(R.id.editText_port);
buttonConnect = (Button) findViewById(R.id.Connect_button);

buttonConnect.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){

String ip = editTextAddress.getText().toString();
int port = Integer.parseInt(editTextPort.getText().toString());

//udpclient = new Udpclient(ip,port);
//udpclient.start();

Intent intent = new Intent(MainActivity.this,SecondActivity.class);

intent.putExtra("ip", ip);
intent.putExtra("port", port);

startActivity(intent);

}
});
}
}

Answer

Your message variable is empty, causing a NULL pointer exception.