Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android应用程序和visual studio终端之间的TCP连接_Android_Visual Studio_Tcp_Communication - Fatal编程技术网

Android应用程序和visual studio终端之间的TCP连接

Android应用程序和visual studio终端之间的TCP连接,android,visual-studio,tcp,communication,Android,Visual Studio,Tcp,Communication,我正在尝试用我的android应用程序作为客户端实现tcp通信。服务器已经在VisualStudio C++中使用库 WS2TCPIP.H实现。然后,客户端应发送一个字符串供服务器接收,然后再次返回。我已经用同样在VisualStudio中实现的客户机测试了服务器,并且我能够发送一条服务器收到然后返回的消息。见下图 更新代码 ControlFragment.java public View onCreateView(@NonNull LayoutInflater inflater, @Null

我正在尝试用我的android应用程序作为客户端实现tcp通信。服务器已经在VisualStudio C++中使用库<代码> WS2TCPIP.H</代码>实现。然后,客户端应发送一个字符串供服务器接收,然后再次返回。我已经用同样在VisualStudio中实现的客户机测试了服务器,并且我能够发送一条服务器收到然后返回的消息。见下图

更新代码

ControlFragment.java

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_control, container, false); //This inflates the control_fragment layout to this java class.

    startBtn = view.findViewById(R.id.startBtn);
    socketString = view.findViewById(R.id.socketString);

    startBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            BackgroundTask b = new BackgroundTask();
            b.execute("Hello");
        }
    });

    return view;
}

class BackgroundTask extends AsyncTask<String, Void, Void> {
    Socket s;
    DataOutputStream dos;
    String ip, message;


    @Override
    protected Void doInBackground(String... params) {
        ip = "192.168.87.103";
        message = params[0];
        try {
            s = new Socket(ip, 9700);
            dos = new DataOutputStream(s.getOutputStream());
            dos.writeUTF(message);
            dos.close();
            s.close();

        } catch (UnknownHostException e) {
            Log.d(TAG, "doInBackground: UnknownHostException" + e.toString());
        } catch (IOException e) {
            Log.d(TAG, "doInBackground: IOexception" + e.toString());
        } catch (Exception e) {
            Log.d(TAG, "doInBackground: Exception" + e.toString());
        }

        return null;
    }
}
public View onCreateView(@NonNull LayoutInflater充气机、@Nullable ViewGroup容器、@Nullable Bundle savedInstanceState){
View View=inflater.inflate(R.layout.fragment_控件,容器,false);//这将控件_片段布局充气到这个java类中。
startBtn=view.findviewbyd(R.id.startBtn);
socketString=view.findviewbyd(R.id.socketString);
startBtn.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
BackgroundTask b=新的BackgroundTask();
b、 执行(“你好”);
}
});
返回视图;
}
类BackgroundTask扩展了AsyncTask{
插座;
数据输出流dos;
字符串ip,消息;
@凌驾
受保护的Void doInBackground(字符串…参数){
ip=“192.168.87.103”;
message=params[0];
试一试{
s=新插座(ip,9700);
dos=新的DataOutputStream(s.getOutputStream());
dos.writeUTF(消息);
dos.close();
s、 close();
}捕获(未知后异常e){
Log.d(标记“doInBackground:UnknownHostException”+e.toString());
}捕获(IOE异常){
Log.d(标记“doInBackground:IOexception”+e.toString());
}捕获(例外e){
d(标记“doInBackground:Exception”+e.toString());
}
返回null;
}
}
现在,我看到在我按下按钮后,客户端已连接到服务器,但它立即断开连接,如下图所示

我认为错误在下面的代码中。服务器似乎没有收到任何东西

int bytesReceived = recv(clientSocket, buf, 4096, 0); //Returns the number of bytes received
    if (bytesReceived == SOCKET_ERROR)
    {
        cerr << "Error in recv(). Qutting" << '\n' << endl;
        closesocket(clientSocket);
        WSACleanup();
    }
    if (bytesReceived == 0)
    {
        cout << "Client disconnected " << '\n' << endl;
        break;
    }
int bytesReceived=recv(clientSocket,buf,4096,0)//返回接收到的字节数
if(bytesReceived==套接字错误)
{

cerr
String message=voids[0];
这不会告诉我们任何信息。我们不知道您发送的是什么。请使用真实文本。
socketString.getText().toString()
从这里开始。我们知道输入了什么。或者socketstring将是什么。如果无法连接,则会出现IOException。请从logcat发布相关部分。我在运行时没有收到任何IOException。我尝试发送的是用户从EditText.Sry键入的字符串,但这是我第一次正在尝试在应用程序和Visual Studio终端之间实现TCP连接您正在尝试发送用户键入的字符串,这一点很清楚。只是我们不知道键入了什么。因此,如果您请求帮助,您可以更好地发布代码,如
messageSender.execute(“现在我们知道您在做什么”)
但如果连连接都无法连接,那就无关紧要了,你应该有某种例外。