我有两个程序Node-A和Node-B,其中Node-A正在向Node-B发送一些char *数据,而Node-B正在向Node-A发送ack(char *数据)。即,clientNodeA_thread正在向Node-B的serverNodeB_thread发送数据。代码如下:
NodeA上
Object o = hmap.get(key);
String stringVal = (o == null) ? null : o.toString()
节点B
int main()
{
pthread_t clientNodeA, serverNodeA;
usleep(3000000);
pthread_create(&clientNodeA, NULL, clientNodeA_thread, "clientNodeA");
pthread_create(&serverNodeA, NULL, serverNodeA_thread, "serverNodeA");
usleep(100000000);
pthread_join(clientNodeA, NULL);
pthread_join(serverNodeA, NULL);
return 0;
}
void* clientNodeA_thread(void* pString)
{
int connSock, in, i, ret, flags;
struct sockaddr_in servaddr, NodeAaddr;
struct sctp_status status;
char buffer[MAX_BUFFER+1];
/* Sample input*/
strncpy(buffer, "FrmNodeAClt", 12);
buffer[12]='\0';
connSock = socket( AF_INET, SOCK_STREAM, IPPROTO_SCTP );
if(connSock == -1)
die("socket()");
#if 0
bzero( (void *)&NodeAaddr, sizeof(NodeAaddr) );
NodeAaddr.sin_family = AF_INET;
NodeAaddr.sin_port = htons(MY_PORT_NUM_NodeA2);
NodeAaddr.sin_addr.s_addr = inet_addr( "127.0.0.1" );
#endif
bzero( (void *)&servaddr, sizeof(servaddr) );
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(MY_PORT_NUM_NodeB);
servaddr.sin_addr.s_addr = inet_addr( "127.0.0.1" );
ret = connect( connSock, (struct sockaddr *)&servaddr, sizeof(servaddr) );
if(ret == -1)
die("connect()");
ret = sctp_sendmsg( connSock, (void *)buffer, (size_t)strlen(buffer),
NULL, 0, 0, 0, 0, 0, 0 );
return 0;
}
void* serverNodeA_thread(void* pString)
{
int listenSock, connSock, ret, in , flags, i;
struct sockaddr_in servaddr;
struct sockaddr_in src_addr;
struct sctp_initmsg initmsg;
int addr_len = 0;
listenSock = socket( AF_INET, SOCK_STREAM, IPPROTO_SCTP );
bzero( (void *)&servaddr, sizeof(servaddr) );
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr( "127.0.0.1" );
servaddr.sin_port = htons(MY_PORT_NUM_ENB2);
ret = bind( listenSock, (struct sockaddr *)&servaddr, sizeof(servaddr) );
/* Specify that a maximum of 5 streams will be available per socket */
memset( &initmsg, 0, sizeof(initmsg) );
initmsg.sinit_num_ostreams = 5;
initmsg.sinit_max_instreams = 5;
initmsg.sinit_max_attempts = 4;
ret = setsockopt( listenSock, IPPROTO_SCTP, SCTP_INITMSG,
&initmsg, sizeof(initmsg) );
listen( listenSock, 5 );
while( 1 ) {
char buffer[MAX_BUFFER + 1];
int len ;
bzero(buffer, MAX_BUFFER + 1);
printf("Awaiting a new connection\n");
connSock = accept( listenSock, (struct sockaddr *)NULL, (int *)NULL );
if(connSock == -1)
die("accept()");
else
printf("New client connected....\n");
addr_len = sizeof (src_addr);
recvfrom(connSock, buffer, sizeof(buffer), 0, &src_addr, &addr_len);
printf("Received data from NodeB : %s\n", (char*)buffer);
}
}
我能够将数据从Node-A客户端发送到Node-B服务器,Node-B也能够发送Ack数据(在Wireshark中验证),但它没有到达Node-A接收代码。
这方面的任何帮助都会非常有帮助。
答案 0 :(得分:0)
问题解决了!我刚刚移动了NodeA的套接字创建并将部分绑定到main函数,以便客户端和服务器线程共享它,客户端和服务器线程只会执行发送和接收功能。
谢谢@Joachim Pileborg