我正在尝试在桌面程序(C / C ++,目前只有命令行)和Android设备之间构建客户端/服务器模型,后者将用于稍后发送数据对象。
我是客户/服务器编程的全新手。作为一个开始,我设法建立一个简单的聊天工作连接,但是有一些我很难理解的错误。客户端只能发送一次消息,服务器根本无法响应。
你有什么想法吗?也许是缓冲区?还是连接本身?我花了几个小时试图理解,但我无法弄清楚。 以下是代码(仅相关部分):
服务器:
int main (int argc, char *argv[]) {
int sockfd, newsockfd;
int pid;
int portno;
socklen_t clilen;
int n;
int portNum = 1500;
char buffer[2048];
struct sockaddr_in serv_addr, cli_addr;
struct sockaddr_in {
short sin_family; /* must be AF_INET */
u_short sin_port;Server:
struct in_addr sin_addr;
char sin_zero[8]; /* Not used, must be zero */
};
if (argc < 2) {
fprintf(stderr,"Server: ERROR, no port provided");
exit(1);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
error("Server: ERROR opening socket");
}
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(portno);
serv_addr.sin_addr.s_addr = INADDR_ANY;
if(bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
error ("Server: Error on binding");
}
if (listen(sockfd, 1) != 0) {
perror("listen");
exit(EXIT_FAILURE);
}
clilen = sizeof(cli_addr);
while (1) {
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0) error("ERROR on accept");
bzero(buffer, 2048);
n = read(newsockfd, buffer, 2047);
if (n < 0) error("ERROR in reading");
printf("Here is the message: %s\n",buffer);
n = write(newsockfd,"I got your message", 18);
if (n < 0) error("ERROR in writing");
}
return 0;
}
客户端:
public class AndroidClientActivity extends AppCompatActivity {
private static final String hostname = "127.0.0.1";
private static final int portnumber = 1500;
private static final String debugString = "debug: ";
Socket socket = null;
private EditText textField;
private Button sendButton;
private String message = "";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.android_client_activity);
textField = (EditText) findViewById(R.id.edit);
sendButton = (Button) findViewById(R.id.send_msg);
new Thread() {
@Override
public void run() {
Log.i(debugString, "Running");
//Connecting
Log.i(debugString, "Trying to connect...");
//connect
try {
socket = new Socket(hostname, portnumber);
Log.i(debugString, "Connected");
} catch (IOException e) {
e.printStackTrace();
Log.e(debugString, e.getMessage());
}
//Send message to server
sendButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
message = textField.getText().toString();
textField.setText("");
Log.i(debugString, "On click detected");
//send and get msg to server
try {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
bw.write("This is a message from the client: " + message);
bw.newLine();
bw.flush();
Log.i(debugString, "Client: Message sent.");
} catch (IOException e) {
e.printStackTrace();
Log.e(debugString, e.getMessage());
}
}
});
try {
//Get message from the server
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream())); //get the content from the socket's input stream
String msg = br.readLine();
System.out.println("Server: " + msg);
Log.i(debugString, "Client: message received from server." + msg);
} catch (IOException e) {
e.printStackTrace();
Log.e(debugString, e.getMessage());
}
}
}.start();
}
}