Java 使用套接字通过wifi发送文本会导致应用程序崩溃

Java 使用套接字通过wifi发送文本会导致应用程序崩溃,java,android,sockets,Java,Android,Sockets,我正试图通过wifi从手机向电脑发送短信,但我的应用程序一直崩溃,我不知道为什么。以下是导致崩溃的代码: try { Socket socket = new Socket(RecieverIP,1755); DataOutputStream DOS = new DataOutputStream(socket.getOutputStream()); DOS.writeUTF(String.valueOf(progressvalue)); socket.close(); } catch (

我正试图通过wifi从手机向电脑发送短信,但我的应用程序一直崩溃,我不知道为什么。以下是导致崩溃的代码:

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();
 }    
在我的电脑端:

 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();}

        }
    }
如果我运行pc代码,它总是只到达输出:“Socket created”

应用程序的全部代码是:

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();

    }
}

为什么我的应用程序会崩溃?我如何修复它?提前感谢

您可能会遇到
networkMainThreadException
异常。当您在主线程上进行联网时,会引发此异常。Android操作系统禁止这样做:

在Android中,您没有在主线程上进行联网。看到这个:@ErvinSzilagyi好吧,谢谢,你知道为什么吗?这是安卓团队的设计决定。可能不会通过锁定主线程来冻结你的应用程序。再次感谢,如果你想回答而不是评论,这样我就可以投票给你,那就太好了。我也写了一个答案。