PC側(c+)とandroid側データ通信(テキスト伝送)

3800 ワード

#include #include #pragma comment(lib,"ws2_32.lib") using namespace std;
int main()  {     WORD w_rep = MAKEWORD(2, 2);     WSADATA wsadata;     SOCKET client;     SOCKADDR_IN server;     char message[1024];
    WSAStartup(w_rep, &wsadata);//初期化バイト
         server.sin_family = AF_INET;     server.sin_port = htons(5000);     server.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");     client = socket(AF_INET, SOCK_STREAM, 0);
    connect(client, (SOCKADDR *) & server, sizeof(SOCKADDR_IN));
    while (true)     {         cout<         cin >> message;         send(client, message, strlen(message),0);         break;     }
    closesocket(client);     WSACleanup();     return 0; } Androidエンドプログラム、テキスト転送
package arcsoft.com.c;


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class MainActivity extends AppCompatActivity {
    public TextView jieshou;
    public int i=0;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        jieshou=(TextView)findViewById(R.id.receive);
        SocketServerThread a= new SocketServerThread();
        a.start();
    }


    public class SocketServerThread extends Thread {
        private Socket client;
        @Override
        public void run() {
            try {
                ServerSocket serverSocket = new ServerSocket(9000);
                while (true) {
                    client = serverSocket.accept();
                    new Thread(new SocketReadThread(new BufferedInputStream(client.getInputStream()))).start();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        class SocketReadThread implements Runnable {

            private BufferedInputStream in;

            public SocketReadThread(BufferedInputStream inStream) throws IOException {
                this.in = inStream;
            }

            public void run() {
                try {
                    while (true) {
                        try {
                            if (!client.isConnected()) {
                                break;
                            }

                            final String currCMD = readMsgFromSocket(in);
                            jieshou.post(new Runnable() {
                                @Override
                                public void run() {
                                    jieshou.setText(currCMD);
                                }
                            });
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    in.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            //  PC        
            private String readMsgFromSocket(InputStream in) {
                String msg = "";
                byte[] tempbuffer = new byte[1024];
                try {
                    int numReadedBytes = in.read(tempbuffer, 0, tempbuffer.length);
                    msg = new String(tempbuffer, 0, numReadedBytes, "utf-8");
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return msg;
            }
        }
    }

}