我正在尝试在 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)
我想我错过了一些东西,我不知道是什么。这是我第一次开发服务器-客户端套接字,我的进步归功于互联网上的一些示例。这是我的代码的一些摘录,我认为的相关部分:
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 中的防火墙,但我仍然没有连接。谢谢你。