1

我用 NDK 在 android 上编写了简单的服务器,它将在 android 模拟器上的端口 8888 上工作,但我无法从主机上的客户端连接到它。

我添加了

<uses-permission android:name="android.permission.INTERNET"/>

在清单中。

我尝试使用 telnet 进行重定向:

 telnet localhost 5554
 redir add tcp:6666:8888

这里的服务器代码:

#include <string.h>
#include <jni.h>

#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>

jstring
Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,
jobject thiz )
{
int socket_desc , client_sock , c , read_size;
struct sockaddr_in server , client;
char* client_message;
int LEN = 512;

client_message = "I am android server";

socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
return (*env)->NewStringUTF(env, "Could not create socket");
}

server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 8888 );

if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{
return (*env)->NewStringUTF(env, "bind failed. Error");
}

listen(socket_desc , 3);

c = sizeof(struct sockaddr_in);

client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c);
if (client_sock < 0)
{
return (*env)->NewStringUTF(env, "accept failed");
}

while( (read_size = recv(client_sock , buf , 2*LEN+1 , 0)) > 0 )
{
write(client_sock , client_message , strlen(client_message));
}

if(read_size == 0)
{
return (*env)->NewStringUTF(env, "Client disconnected");
}
else if(read_size == -1)
{
return (*env)->NewStringUTF(env, "recv failed");
}

return (*env)->NewStringUTF(env, "Server closed");
}

以及主机上的客户端代码:

#include "stdafx.h"
#include <winsock2.h>
#include <windows.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <conio.h>
#include <iostream>

#pragma comment(lib, "Ws2_32.lib")

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{ 

int host_port= 6666;
char* host_name= "localhost";

unsigned short wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );

if ( err != 0 || ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 2 )) {
fprintf(stderr, "Could not find useable sock dll %d\n",WSAGetLastError());
return 0;
}

int hsock;
int * p_int ;
hsock = socket(AF_INET, SOCK_STREAM, 0);
if(hsock == -1){
printf("Error initializing socket %d\n",WSAGetLastError());
return 0;
}

p_int = (int*)malloc(sizeof(int));
*p_int = 1;

if( (setsockopt(hsock, SOL_SOCKET, SO_REUSEADDR, (char*)p_int, sizeof(int)) == -1 )||
(setsockopt(hsock, SOL_SOCKET, SO_KEEPALIVE, (char*)p_int, sizeof(int)) == -1 ) ){
printf("Error setting options %d\n", WSAGetLastError());
free(p_int);
return 0;
}
free(p_int);

struct sockaddr_in my_addr;

my_addr.sin_family = AF_INET ;
my_addr.sin_port = htons(host_port);

memset(&(my_addr.sin_zero), 0, 8);
my_addr.sin_addr.s_addr = inet_addr(host_name);

cout<<"client ready\n";
if( connect( hsock, (struct sockaddr*)&my_addr, sizeof(my_addr)) == SOCKET_ERROR ){
fprintf(stderr, "Error connecting socket %d\n", WSAGetLastError()); getchar();
return 0;
}
cout<<"connected to server\n"; 

char buffer[1024];
int buffer_len = 1024;
int bytecount;

int c;
memset(buffer, '\0', buffer_len);

for(char* p=buffer ; (c=getch())!=13 ; p++){
printf("%c", c);
*p = c;
}

if( (bytecount=send(hsock, buffer, strlen(buffer),0))==SOCKET_ERROR){
fprintf(stderr, "Error sending data %d\n", WSAGetLastError());
return 0;
}
printf("Sent bytes %d\n", bytecount);

if((bytecount = recv(hsock, buffer, buffer_len, 0))==SOCKET_ERROR){
fprintf(stderr, "Error receiving data %d\n", WSAGetLastError());
return 0;
}
printf("Recieved bytes %d\nReceived string \"%s\"\n", bytecount, buffer);

closesocket(hsock);

return 0;
}

块引用

4

1 回答 1

0

解决了!

这是因为我的 Windows 防火墙阻止了此端口(在我的情况下为 6666)并且客户端无法连接到模拟器上的服务器

在 Windows 中解锁端口 - 打开防火墙的额外选项并添加收入连接规则(所有人免费)

于 2013-03-20T13:18:20.190 回答