我正在尝试在运行ubuntu 12.04的C中的服务器和运行在Win7中的Java客户端(EJS)之间建立TCP / IP通信。服务器是更大程序的线程,所以它是用pthread创建的。当我在ubuntu(localhost)中同时使用服务器和客户端时,一切都很顺利,所有功能都很好,我正在进一步配置我的GUI。但是当我在win7的笔记本中使用客户端时,即使我定义了主机ip和端口(/ etc / hosts + / etc / services和C:\ WINNT \ system32 \ drivers \ etc \,我也无法建立连接) hosts + C:\ WINNT \ system32 \ drivers \ etc \ services)
我想我错过了什么,我不知道是什么。这是我第一次开发服务器客户端套接字,我的进步归功于互联网上的一些例子。以下是我的代码的一些摘录,我认为相关的部分:
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中的防火墙,但我仍然没有连接。 谢谢。
答案 0 :(得分:1)
您好,在阅读完服务器代码后,我建议您按如下方式设置服务器套接字的端口:
Direccion.sin_port = htons(25557) // the port have mentioned in java client socket.
答案 1 :(得分:0)
正如您所说,“这是我第一次开发服务器客户端套接字和我的进度”
关闭Windows防火墙,然后重试。因为我对Java知之甚少,所以我唯一可以提出建议。