我必须通过eth0&转发数据。 eth1的。所以我在客户端编写了一个代码,我在其中使用thread打开两个套接字。但是它无法正常工作意味着它没有连接eth1(连接错误)。数据仅通过etho发送 注意:这里我使用两个独立的网络。 这是我的代码。请仔细阅读...
void Socket_1()
{
struct sockaddr_in ser_sock1 ;
u_int yes=1 ;
int setsock_fd ;
ser_sock1.sin_family = AF_INET ;
ser_sock1.sin_port = htons( 1602 ) ;
ser_sock1.sin_addr.s_addr =inet_addr("192.168.1.35")
cl_fd1 = socket(AF_INET,SOCK_STREAM,0) ;
if (cl_fd1 == -1)
{
printf("Socket1 : ERROR\n") ;
return ;
}
/* Bind to socket*/
if( bind(cl_fd1,(struct sockaddr *)&ser_sock1 , sizeof(ser_sock1)) < 0)
{
printf("bind1 : ERROR\n") ;
return ;
}
/* Construct the server sockaddr_in structure */
memset(&ser_sock1, 0, sizeof(ser_sock1)); /* Clear struct */
ser_sock1.sin_family = AF_INET ;
ser_sock1.sin_port = htons( 22008 ) ;
ser_sock1.sin_addr.s_addr = inet_addr("192.168.1.151") ;
/*connect to destination*/
con_fd1 = connect(cl_fd1, (struct sockaddr *)(&ser_sock1), sizeof(ser_sock1));
if(con_fd1 < 0)
{
printf("connect1 : ERROR\n") ;
while(1)
{
}
return ;
}
g_socketCreated = 1 ;
while(g_socketCreated)
{
sprintf( buff, "Mess-%08X", count++ );
send( cl_fd1, buff, strlen(buff), 0x0 ) ;
sleep(1) ;
}
}
void Socket_2(void)
{
struct sockaddr_in ser_sock2, ser_sock3 ;
ser_sock2.sin_family = AF_INET ;
ser_sock2.sin_port = 0 ;
ser_sock2.sin_addr.s_addr = inet_addr("192.168.42.130") ;
cl_fd2 = socket(AF_INET,SOCK_STREAM,0) ;
if (cl_fd2 == -1)
{
printf("Socket2 : ERROR\n") ;
return ;
}
/* Bind to socket*/
if( bind(cl_fd2,(struct sockaddr *)&ser_sock2 , sizeof(ser_sock2)) < 0)
{
printf("bind2 : ERROR\n") ;
return ;
}
/* Construct the server sockaddr_in structure */
memset(&ser_sock3, 0, sizeof(ser_sock3)); /* Clear struct */
ser_sock3.sin_family = AF_INET ;
ser_sock3.sin_port = htons( 22000 ) ;
ser_sock3.sin_addr.s_addr = inet_addr("110.234.115.27") ;
/*connect to destination*/
con_fd2 = connect(cl_fd2, (struct sockaddr *)(&ser_sock3), sizeof(ser_sock3));
if(con_fd2 < 0)
{
printf("connect2 : ERROR\n") ;
return ;
}
g_socketCreated1 = 1 ;
while(1)
{
sprintf( buff1, "soc2---Mess-%08X", count1++ );
send( cl_fd2, buff1, strlen(buff1), 0x0 ) ;
sleep(1) ;
}
}