2013-05-16 33 views
0

我想在运行在Ubuntu 12.04中的C服务器和运行在Win7中的Java(EJS)中的客户端之间建立TCP/IP通信。服务器是一个更大程序的线程,所以它是用pthread创建的。当我在ubuntu(localhost)中同时使用服务器和客户端时,一切都很顺利,所有功能都很好,我正在进一步改进配置我的GUI。但是,当我在我的笔记本电脑上使用win7客户端时,即使定义了主机IP和端口(/ etc/hosts +/etc/services和C:\ WINNT \ system32 \ drivers \ etc \主机+ C:\ WINNT \ system32 \ drivers \ etc \ services)ubuntu(C)和windows(Java)之间的TCP/IP连接

我想我错过了一些东西,我不知道是什么。这是我第一次开发服务器 - 客户端套接字,我的进展得益于互联网中的一些例子。下面是我的一些代码提取物,相关部分,我认为:

在C(Ubuntu的)服务器:

//Initialization, headers and other threads 
. 
. 
. 

void * servidor(void *arg) 
{ 
int Socket_Servidor; 
int Socket_Cliente; 
struct sockaddr_in Direccion; 
struct servent *Puerto; 
socklen_t Longitud_Cliente; 
struct sockaddr Cliente; 
struct timespec now,period; 
int dummy,i,j; 
unsigned long overruns_r; 
int ejecutado =1; 
int terminado =1; 

int A[1];      
int B[2]; 
double C[2]; 

    period.tv_sec=0; 
    period.tv_nsec=PERIOD3; 

    clock_gettime (CLOCK_REALTIME, &now); 
    now.tv_nsec=now.tv_nsec+PERIOD3; 
    dummy=pthread_make_periodic_np (pthread_self(), &now,&period); 
    switch(dummy){ 
    case 0 : 
     break; 
    case ESRCH: 
    printf("thread is invalid \n"); 
    pthread_exit ((void *)-1); 
     break; 
    case ETIMEDOUT : 
    printf("the start time has already passed\n"); 
    pthread_exit ((void *)-1); 
     break; 
    default : 
    printf(" output value not defined \n"); 
     pthread_exit ((void *)-1); 
} 

Socket_Servidor = socket (AF_INET, SOCK_STREAM, 0); //Obtener el descriptor del socket 
if (Socket_Servidor == -1) printf("No se puede crear el socket\n"); 

Puerto = getservbyname ("cpp_java", "tcp"); //Obtener el numero del servicio 25557 
if (Puerto == NULL) printf("BIND fallido\n"); 

Direccion.sin_family = AF_INET; //Tipo de conexion 
Direccion.sin_port = Puerto->s_port; //Servicio a atender 
Direccion.sin_addr.s_addr =INADDR_ANY; //Dirección del cliente (cualquiera) 

if (bind (Socket_Servidor,(struct sockaddr *)&Direccion,sizeof(Direccion))==-1) 
{ 
    printf("BIND fallido\n"); 
    close (Socket_Servidor); 
} 


if (listen (Socket_Servidor, 1) == -1) //Atender llamadas, un cliente en espera 
{ 
    printf("Fallo en 'listen'\n"); 
close (Socket_Servidor); 
} 

//Aceptar la conexion 
Longitud_Cliente = sizeof (Cliente); 
Socket_Cliente = accept (Socket_Servidor, &Cliente, &Longitud_Cliente); 
if (Socket_Cliente == -1) printf ("No se puede abrir socket de cliente\n"); 

while(ejec){ 

// Some actions of the server 
. 
. 
. 

Java客户端(WIN7)

// Custom section of EJS 
public boolean conectar() { 
try { 

    javaSocket = new Socket();//crea socket sin conexion 
    ((Socket)javaSocket).connect(new InetSocketAddress("10.5.3.60",25557),3000);// 3 seg de timeout en la conexion inicial 
    ((Socket)javaSocket).setSoTimeout (8000);// 8 segundos de timeout durante la conexion 

    in = new DataInputStream(((Socket)javaSocket).getInputStream()); 
    out = new DataOutputStream(((Socket)javaSocket).getOutputStream()); 
    ((Socket)javaSocket).setTcpNoDelay (true); 
    if (javaSocket != null) { 
     connected = true; 
     _play(); 
    } 
}catch (java.net.UnknownHostException e) { 
    lastErrorMsg = "Method startTCP: Unknown host." + " " + e.getMessage();    
}catch (SocketTimeoutException e2){ 
    lastErrorMsg = "Method startTCP: Timeout at connect.";    
}catch (java.io.IOException e) { 
    lastErrorMsg = "Method startTCP: Input/output exception." + " " + e.getMessage(); 
}catch (java.lang.Exception e2){ 
    lastErrorMsg = "Method startTCP: No connection to host." + " " + e2.getMessage();   
}  
return connected; 
} 

任何帮助将非常感激。非常感谢您的时间和提前回复

感谢您的意见和解答。我试图lsof -Pni | grep LISTEN,我得到这个:

cupsd 717 root 8u IPv6 10478 0t0 TCP [::1]:631 (LISTEN) 
cupsd 717 root 9u IPv4 10479 0t0 TCP 127.0.0.1:631 (LISTEN) 
dnsmasq 1097 nobody 5u IPv4 11512 0t0 TCP 127.0.0.1:53 (LISTEN) 
ser1 1998 root 3r IPv4 12659 0t0 TCP *:25557 (LISTEN) 

其中SER1是我的服务器程序。我也在Windows中禁用防火墙,但我仍然没有连接。 谢谢。

+1

在Ubuntu的服务器端,为根,列出开放的插槽与听众:'lsof的-Pni | grep LISTEN'并确保你可以在列表中找到你的服务器进程。在这里发布您的服务器程序的输出行。我怀疑服务器进程只在本地(到服务器)端口上进行监听,而不是在以太网接口上进行监听。 – rolfl

+0

我在代码中看不到任何数据传输。你得到什么异常/错误?你可以发布_play()(相关部分)功能吗? –

+0

我编辑了我的问题并添加了lsof -Pni的结果| grep LISTEN,接下来应该检查什么? – user2337385

回答

0

正如你所说的,“这是我第一次开发一个服务器 - 客户端套接字和我的进步”

关闭Windows的防火墙,然后再试一次。因为我对Java不太了解,所以我只能提供建议。读你的服务器代码后

1

您好我可以建议你设置端口服务器套接字如下:

Direccion.sin_port = htons(25557) // the port have mentioned in java client socket.