gpt4 book ai didi

Android 客户端未从 C 服务器套接字获取响应

转载 作者:行者123 更新时间:2023-11-30 17:03:01 24 4
gpt4 key购买 nike

我能够从 Android 客户端将字符串发送到 C 服务器。但我无法将响应从服务器返回到 Android 客户端。Android 客户端未获取数据。我想“ toast ”从 c 服务器获得的响应。

服务器代码(c):

#include <stdio.h>
#include <stdlib.h>

#include <netdb.h>
#include <netinet/in.h>

#include <string.h>

int main( int argc, char *argv[] ) {
int sockfd, newsockfd, portno, clilen;
char buffer[256];
char g[255];
char *message;
struct sockaddr_in serv_addr, cli_addr;
int n;
int p;

/* First call to socket() function */
sockfd = socket(AF_INET, SOCK_STREAM, 0);

if (sockfd < 0) {
perror("ERROR opening socket");
exit(1);
}

/* Initialize socket structure */
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = 5011;

serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr("192.168.1.125");
//serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);

/* Now bind the host address using bind() call.*/
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
perror("ERROR on binding");
exit(1);
}

/* Now start listening for the clients, here process will
* go in sleep mode and will wait for the incoming connection
*/

listen(sockfd,5);
clilen = sizeof(cli_addr);

/* Accept actual connection from the client */
newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen);

if (newsockfd < 0) {
perror("ERROR on accept");
exit(1);
}

/* If connection is established then start communicating */
bzero(buffer,256);
n = read( newsockfd,buffer,255 );

if (n < 0) {
perror("ERROR reading from socket");
exit(1);
}

printf("Here is the message: %s\n",buffer);

/* Write a response to the client */

//int man = 12345;
message ="hello\n";

n = write(newsockfd, message,strlen(message));


return 0;
}

****Android 客户端:****

import android.app.Activity;
import android.graphics.Color;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.text.format.Formatter;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;


public class ScreenTwo extends Activity {
EditText et;
String txt,ssid,key;
WifiManager wifiManager;
private Socket socket;

private Socket clientSocket;
private BufferedReader input;


static final int SERVERPORT = 5047;
private static final String SERVER_IP = "192.168.1.125";


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen2);
new Thread(new ClientThread()).start();


}


public void onClick(View v) {

try {
EdritText et = (EditText) findViewById(R.id.tb1);
Sting str = et.getText().toString();
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())),
true);
out.println(str + "\n");
out.flush();


BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String read = input.readLine();
Toast.makeText(this, read, Toast.LENGTH_LONG).show();

//ready to receive data from server
//BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//System.out.println(in.readLine());
//Toast.makeText(this, in.readLine(), Toast.LENGTH_LONG).show();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}

public void onEnterBtn(View v)
{
//Toast.makeText(this, "Clicked on Button", Toast.LENGTH_LONG).show();
et=(EditText)findViewById(R.id.tb1);
txt=et.getText().toString();
Toast.makeText(this, txt, Toast.LENGTH_LONG).show();

Button disp = (Button)findViewById(R.id.dispbtn);
disp.setBackgroundColor(Color.parseColor(txt));



}

public void onbuttonwifi(View v)
{
//Toast.makeText(this, "Clicked on Button", Toast.LENGTH_LONG).show();
WifiManager wifiMgr = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
int ip = wifiInfo.getIpAddress();
String ipAddress = Formatter.formatIpAddress(ip);
Toast.makeText(this, ipAddress, Toast.LENGTH_LONG).show();

}

class ClientThread implements Runnable {
@Override
public void run() {
try {
InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
socket = new Socket(serverAddr, SERVERPORT);

} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}

}

提前致谢:)

最佳答案

 String read = input.readLine();

客户端尝试读取一行。但服务器没有发送一行

n = write(newsockfd, g,p);

发送前向字符串变量g添加"\n"。在确定p之前。

关于Android 客户端未从 C 服务器套接字获取响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36302654/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com