Sorry, but I'm new to both c++ and socket. So, I can't understand both of you. For more details, I post some code here. I used MFC to develop GUI, so, the extracted code is not very neat and for simplicity, I don't mention some trivial code.
This is code of AppA
Code:
// create socket to send message
socketa = socket(AF_INET, SOCK_DGRAM, PF_UNSPEC);
char opt[2];opt[0] = 1;opt[1] = 0;
setsockopt(socketa, SOL_SOCKET, SO_BROADCAST, opt, sizeof(opt)); // Broadcast socket
// use broadcast address
IN_ADDR addr; // 255,255,255,255; don't mention here
SOCKADDR_IN sendaddress;
sendaddress.sin_addr = addr;
sendaddress.sin_family = AF_INET;
sendaddress.sin_port = htons(8009);
// this is code to send message
char buf[] = "hello world!"; // in fact, i get it from a textbox
int len = (int) strlen(buf);
sendto(socketa, (char FAR *)buf, len, 0,(const struct sockaddr FAR *)&sendaddress, sizeof(sendaddress));
This is code of AppB
Code:
// create Non-Blocking socket to send message
socketb = socket(AF_INET, SOCK_DGRAM, PF_UNSPEC);
unsigned long lNB = 1; // Non-Blocking socket
ioctlsocket(socketb, FIONBIO, &lNB);
// set address to local address
SOCKADDR_IN receiveaddress;
receiveaddress.sin_addr = localhostaddress(); // don't mention here
receiveaddress.sin_family = AF_INET;
receiveaddress.sin_port = htons(8009);
bind(socketb,(const SOCKADDR FAR*)&receiveaddress, sizeof(receiveaddress));
// receive message
recvfrom(socketb, (char FAR*)buf, BUFFER_SIZE, 0, NULL, NULL);
This is how my program run, but only one-way communication. I tried to make it two-way, using the same socket. But how can I make it? I try to make some code, but it always throws very-hard-to-read-and-debug Exception. In fact, I think I can't debug when any Exception is thrown in C++ (
Can you help me? I think it's a basic technique with someone who familiar with c++ and socket. Please help me out with some code. Thanks!!!