I'm trying to send a text from my phone to my pc over wifi but my app keeps crashing and I have no clue why. Here is the code which crashes:
try {
Socket socket = new Socket(RecieverIP,1755);
DataOutputStream DOS = new DataOutputStream(socket.getOutputStream());
DOS.writeUTF(String.valueOf(progressvalue));
socket.close();
} catch (IOException e)
{
e.printStackTrace();
}
and on my pc's end:
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author fares Part of a project to control the led brightness on a pi, the job of this program is to receive a text over wifi and then launch a python program and pass the brightness as a parameter
*/
public class WebSocketReciever {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
System.out.println("Program started");
String msg_received;
System.out.println("Creating socket");
ServerSocket socket = new ServerSocket(1755);
System.out.println("Socket created");
Socket clientSocket = socket.accept(); //This is blocking. It will wait.
System.out.println("Client socket created");
while(true){
System.out.println("Reading data");
DataInputStream DIS = new DataInputStream(clientSocket.getInputStream());
msg_received = DIS.readUTF();
System.out.println(msg_received);
clientSocket.close();
socket.close();}
}
}
If I run the pc code it always only reaches the output: "Socket created"
The whole code for the app is:
package com.example.fares.ledslider;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import static com.example.fares.ledslider.R.id.seekBar;
public class MainActivity extends AppCompatActivity {
private static TextView textView;
private static SeekBar seek_Bar;
private static String RecieverIP;
private static EditText IPText;
public void seekbarmethod(){
seek_Bar = (SeekBar) findViewById(seekBar);
textView = (TextView) findViewById(R.id.textview);
seek_Bar.setMax(100); //Max value of the seekbar
seek_Bar.setProgress(50);//Initial seekbar value
textView.setText("Brightness = " +seek_Bar.getProgress() + " %"); //Notify user of percentage brightness
seek_Bar.setOnSeekBarChangeListener(
new SeekBar.OnSeekBarChangeListener(){
int count =0;
int progressvalue;
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
progressvalue = i;
textView.setText("Brightness = " + progressvalue + " %");
// Toast.makeText(MainActivity.this,"Change in progress" + count,Toast.LENGTH_SHORT).show();
//count +=1;
//Send data from app to pc
try {
Socket socket = new Socket(RecieverIP,1755);
DataOutputStream DOS = new DataOutputStream(socket.getOutputStream());
DOS.writeUTF(String.valueOf(progressvalue));
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
Toast.makeText(MainActivity.this,"Change initiated",Toast.LENGTH_SHORT).show();
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
Toast.makeText(MainActivity.this,"Change ended",Toast.LENGTH_SHORT).show();
count = 0 ;
}
}
);
}
public void IPBtnMethod(View v){
IPText = (EditText) findViewById(R.id.IPBox);
RecieverIP = IPText.getText().toString();
Toast.makeText(MainActivity.this,"IP = " + RecieverIP,Toast.LENGTH_SHORT).show();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
seekbarmethod();
}
}
Why does my app crash and how can I fix it? Thanks in advance