473,405 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

problem changing code from TCP to UDP

i created a client server program in MFC using TCP and that worked fine. But i want the server to work on any computer so for the client to connect to the server i need to use broadcasting which can only be done in UDP. so i tried changing my code but i'm having a lot of problems. can someone please look at the code and tell me if i'm missing something (never used UDP before).

heres the code for server
when i run the program the message "error with sendto: 10047" displays
Expand|Select|Wrap|Line Numbers
  1. WSADATA wsaData; 
  2.     WSAStartup(MAKEWORD(2,2), &wsaData);
  3.  
  4.     int port = 7171;
  5.     if (param)
  6.         port = reinterpret_cast<short>(param);
  7.  
  8.       SOCKET s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  9.     if (s == -1)
  10.     {
  11.         closesocket(s);
  12.         return 1;
  13.     }
  14.  
  15.     sockaddr_in brdcastaddr;
  16.     int len = sizeof(brdcastaddr);
  17.     char sbuf[1024]; 
  18.     brdcastaddr.sin_family = AF_INET;
  19.     brdcastaddr.sin_port = htons(port);
  20.     brdcastaddr.sin_addr.s_addr = (INADDR_ANY);
  21.  
  22.  
  23.     char opt = 1; 
  24.  
  25.     int bind_ret = bind(s, (sockaddr*)&brdcastaddr, sizeof(brdcastaddr));           
  26.     if (bind_ret == -1)
  27.     {
  28.         CString text;
  29.         text.Format(_T("ERROR binding: %d"), WSAGetLastError());
  30.         AfxMessageBox(text);
  31.         closesocket(s);
  32.         return 1;
  33.     }
  34.  
  35.     setsockopt(s, SOL_SOCKET, SO_BROADCAST, (char*)&opt, sizeof(char));
  36.     memset(&brdcastaddr,0, sizeof(brdcastaddr));
  37.  
  38.  
  39.  
  40.     int ret = sendto(s, sbuf, strlen(sbuf), 0, (sockaddr*)&brdcastaddr, len);
  41.     if(ret < 0)
  42.     {
  43.         CString text;
  44.         text.Format(_T("ERROR with sendto: %d"), WSAGetLastError());
  45.         AfxMessageBox(text);
  46.         return 1;
  47.     }
  48.  
  49.     int listen_ret = listen(s, 5); 
  50.     if (listen_ret == -1)
  51.     {
  52.         CString text;
  53.         text.Format(_T("ERROR listening: %d"), WSAGetLastError());
  54.         AfxMessageBox(text);
  55.         closesocket(s);
  56.         return 1;
  57.     }
  58.  
  59.     while (true)
  60.     {
  61.         sockaddr_in client_addr;
  62.         int len = sizeof(client_addr);
  63.         SOCKET client_sock = accept(s, (sockaddr*)&client_addr, &len);
  64.  
  65.         ClientInfo info;
  66.         info.sock = client_sock;
  67.         info.addr = inet_ntoa(client_addr.sin_addr);
  68.         {
  69.             Mutex<CriticalSection>::Lock lock(client_cs);
  70.             clients.push_back(info);
  71.         }
  72.  
  73.         unsigned tid;
  74.         _beginthreadex(NULL, 0, tcp_servers_client, reinterpret_cast<void*>(client_sock), 0, &tid);
  75.     }
  76.  
  77.     closesocket(s);
  78.     return 0;
  79.  
Mar 20 '13 #1
1 1372
Banfa
9,065 Expert Mod 8TB
Firstly before you do anything else look up "beej guide to network programming" in your favourite web search engine and read it.

The problem with what you are trying to do is that it is not simple due to the different natures of TCP and UDP sockets.

A TCP socket is a byte stream, that is the data is not sent in distinct blocks, just because you sent 3 lots of 10 bytes doesn't mean you will receive 3 lots of 10 bytes. You might receive 1 lot of 30 bytes or 30 lots of 1 byte or anything in-between. But you are guaranteed that if you are receiving data then it is the right data and arriving in the order it was transmitted. A TCP socket is connected, that is it is a point to point connection. The first thing you do is set up the connection.

A UDP socket sends datagrams, that is the data is sent in distinct blocks which are limited in size. A UDP socket is not a connection, the is no feed back or guarantee of delivery in any order, either you do or don't receive the block of data as a distinct object with no reference to any other object transmitted. When you transmit a UDP datagram you just send it out and hope that someone is listening for it, no connection is made. This means that when you receive you just listen out for an incoming UDP datagram with bothering to listen for a connection because you wont get one. And that means that calling listen and accept have no meaning for a UDP socket.

Like I said read beej's guide to network programming it will explain everything.
Mar 20 '13 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: John N. | last post by:
Hi All, Here I have a linked list each containing a char and is double linked. Then I have a pointer to an item in that list which is the current insertion point. In this funtion, the user...
2
by: Mrozik | last post by:
Hi! i read and update data from database that have ISO-8859-2 code page set default my application (windows app) run in polish code page (win1250) how can i read data and show them to user in...
0
by: Scott P. | last post by:
I'm creating an app using ASP .NET (my second app so bear with me here) that basically builds a PDF file based on a bunch of user selections. I have a page which displays a series of checkboxs...
4
by: Simon Harvey | last post by:
Hi all, Am I being really stupid here: myDropDown.SelectedIndex = 2 I think this line should set the dropdown control's selected item to 2. But nothing seems to be happening on the page....
3
by: Mike | last post by:
In VB 6, you could change most code while your project was in Break mode. I am targeting the Pocket PC, and VB .NET 2003 is not allowing me to change code while debugging. Is this true for all VB...
0
by: Ernst Elzas | last post by:
I need to make certain changes to multiple pages within the site under development, what's the easiest way to do that? If both the code to be replaced in a page and it's replacement contain only...
5
by: John | last post by:
Hi I am trying to set a new password using the following code; Dim u As MembershipUser = Membership.GetUser(UserName) Dim OldPassword As String OldPassword = u.GetPassword If...
8
by: mike_solomon | last post by:
I have a button <input type="submit" name="Delete" value="Delete"> This code can not be changed I want to use Javascript to change the type I tried:
2
by: pdickson | last post by:
Hi - I am using custom textboxes and I need to be able to set the Text on txb1 based on the Text of txb2. I have simplied my code to the following: Public Class customtextbox Inherits...
4
by: heitikimairim | last post by:
Hi all, I need to design an XSL to convert this xml: <Fields> <Field Name="DebitCredit" Type="invoicecredit">Debit Factuur</Field> </Fields> into:
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.