Advertisement

12.09.2007 at 08:19AM PST, ID: 23011625
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Visual C++ Winsock

Tags: winsock, visual, udp
Hi experts,
Heres the scenario: I'm doing a communications module which will be used by other users to send string messages and object files (i.e. images jpg). Im using UDP mode to support this communcation.
Eventually i.e. Module A will open a thread to listen for incoming data via socket A where Module B and Module C can send simultaneously to A without hogging the bandwidth like streaming does?.

I am having problems with the following:
1) How do I send data with size > 4096 bytes via UDP, fragment the data? how do i do it?
2) How do i "serialize" a 1MB image file into the proper format for data transfer. e.g. for string I can truncate the message but for objects?
Start your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: vinawy
Solution Provided By: BillBach
Participating Experts: 3
Solution Grade: B
Views: 102
Translate:
Loading Advertisement...
12.09.2007 at 04:37PM PST, ID: 20438784

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
12.09.2007 at 06:34PM PST, ID: 20439178

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
12.10.2007 at 04:05AM PST, ID: 20440804

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
12.10.2007 at 05:42AM PST, ID: 20441203

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
12.10.2007 at 06:06AM PST, ID: 20441358

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
12.10.2007 at 06:14AM PST, ID: 20441444

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
12.10.2007 at 06:54AM PST, ID: 20441898

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
12.10.2007 at 10:51AM PST, ID: 20444034

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
12.10.2007 at 11:11AM PST, ID: 20444180

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
12.09.2007 at 04:37PM PST, ID: 20438784

Rank: Guru

1) How do I send data with size > 4096 bytes via UDP, fragment the data? how do i do it?
Yes, fragment the data, but consider that most media allows frames of just 1536 bytes
To fragment the data, typical approach is to include a header with session number and sequence number, to avoid bad sequencing of 2 fragments of different sessions.
2) How do i "serialize" a 1MB image file into the proper format for data transfer. e.g. for string I can truncate the message but for objects?
XML serialization is frequently used to solve the format problematic.
Assisted Solution
 
12.09.2007 at 06:34PM PST, ID: 20439178
For fragmentation, do you have a sample code as to how I can implement that in Visual C++ using winsock?
Thanks.
 
12.10.2007 at 04:05AM PST, ID: 20440804
Fragmentation is feature of IP protocol. U can set DF bit in flags to zero -> fragmentation is allowed. It is 4. parameter of sento() function. There are some useful documents http://www.sockets.com/winsock2.htm#Docs and here http://www.networksorcery.com/enp/protocol/ip.htm#Flags
Assisted Solution
 
12.10.2007 at 05:42AM PST, ID: 20441203
I was hoping someone could shed some insight on my code implementation on how I can fragment/send data more than 1500bytes. I read somewhere on the net that says that the MTU on n the local machine, if using the loopback (127.0.0.1), it can handle 8K but on Ethernet, it can only handle 1500 bytes with a header.

Also I need my receiver to be able to handle receipts in non-blocking mode.

Many Thanks.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
/* Send */
	while(total < len) {
        n = sendto(sd, data+total, bytesleft, 0, (struct sockaddr *)&client, client_length);
        if (n == -1){ 
			fprintf(stderr, "Error Sending Datagram.\n");
			closesocket(sd);
			WSACleanup();
			return -1;
			break; 
		}
        total += n;
        bytesleft -= n;
    }
	sprintf(msg,"[Sucessfully sent %d bytes of data - %s]\n [Msg:%s] \n", len, ctime(&current_time), data);
return 0;
}
 
 
/* Receive */
	bytes_received = recvfrom(sd, buffer, BUFFER_SIZE, 0, (struct sockaddr *)&client, &client_length);
	if (bytes_received < 0){
		sprintf(msg,"Error Code %d.\n",WSAGetLastError());
		return -1;
	}
Open in New Window
 
12.10.2007 at 06:06AM PST, ID: 20441358
This is the updated recv function where I tried to implement a non blocking mode by using ioctlsocket(sock, FIONBIO, &flag); where flag is a "long flag = 1;"
Please advice me if its the correct way to do it. Thank you.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
/* Receive 
	Setting socket to non blocking mode.
	*/
	bool poll = true;
	ioctlsocket(sock, FIONBIO, &flag);
 
	while (poll){
		bytes_received = recvfrom(sd, buffer, BUFFER_SIZE, 0, (struct sockaddr *)&client, &client_length);
			if(WSAGetLastError()==10035 || WSAGetLastError()==WSAEWOULDBLOCK){
				poll= true;
				break;
			else if (bytes_received < 0){
				sprintf(msg,"Error Code %d.\n",WSAGetLastError());
				poll= false;
				return -1;
				}
			}
	}
Open in New Window
 
12.10.2007 at 06:14AM PST, ID: 20441444
It may be a more important question to decide if you really want to be using UDP for communications like this.  Sure, you avoid a lot of the headaches of setting up a TCP session,  but you should remember that UDP is designed as a non-serialized, non-guaranteed-delivery protocol.  This means that if you start sending packets, you are NOT guaranteed that all packets will get to their destination (it is a "best-effort" delivery only).  This also means that there is no guarantee that all packets get there in order, either.  As such, any time you must fragment the data, you run the risk of getting a completely useless data set on the receiving computer.  This would be very risky and could completely render images (especially compressed images) useless to the receiver.

Before going too far down the path of figuring out how to do this with UDP, I would urge you to review the functional specifications for the project from a more basic design standpoint first.  If you are going to be habitually sending elements larger than 1K, then a TCP session will be vastly superior, and it will likely address most of the issues you would have automatically.  

In short, don't reinvent the wheel if you don't have to.
Assisted Solution
 
12.10.2007 at 06:54AM PST, ID: 20441898
There is is only one reason to use UDP instead of TCP - broadcast (saving net-traffic) and 12B smaler header. If it is not your case, BillBach is right :). On the Ethernet is default MTU about 1500B if you have better cabels, it's possibility to get more.
Packet fragmenting is L3 job. And you are programming on L4. U can fragmenting data, but by your self in application.

When you send UDP packet witch is 5000B length and you have allowed packet fragmentation this will happen:
Packet will be cut into ceil(5000/(your_pc_mtu-8B-header)) by IP. then will be sent to the destination. In this case its about 4 packets. If receiver receive all 4 packet without errors, packet will be assembled into original UDP with 5kB and forwarded to your application.

The problem is sending 4 successive packets.
Assisted Solution
 
12.10.2007 at 10:51AM PST, ID: 20444034
Thanks for the advice BillBach, Michal_sjx and also jaime_olivares,

My struggle here is i'm restricted to use only winsocks visual c++ and I need advice on how to do the following:

1) Fragmentation: Besides coming out with a algorithm to truncate string message into proper sizes before sending, how i can make use of winsock commands/functions(if any). What is the proper way of tracking the fragment/packet ID and re-ordering them using UDP?

2)Non Blocking Recv: Basically I've set socket to non blocking (ioctlsocket(sd, FIONBIO, &flag) where flag is non zero), is that enough for a complete implementation of non blocking as i came across functions like WSAAsyncSelect and WSAEventSelect which is also related to non-blocking.

Hoping for more insights before I close this. Thanks alot for your help.
 
12.10.2007 at 11:11AM PST, ID: 20444180
1) There is no ordering or packet ID's provided by UDP -- it is a very simplistic protocol and not designed for guaranteed delivery or proper ordering.  As such, you would need to design and implement your own ordering scheme and include it as a header in the data packet itself.  (Essentially, you'll rewrite the TCP stack yourself.)

The way to do this is to design your own Network Layer header (and you can certainly steal from the TCP spec if it makes sense) and include that in every UDP packet you send.  Including elements such as Total Message Length, current packet length, current packet number, etc.

Then, you'd need to fragment each message on your own.  A VERY simplistic approach would be something like this:

    BytesLeft=MessageLength;
    for(FragmentNumber=0,ptr=Message;BytesLeft>0;FragmentNumber++)
    {
        BytesToSend=min(BytesLeft,MaximumFragmentSize);
        memcpy(Fragment,ptr,BytesToSend);
        SendUDPFragment(Fragment,BytesToSend,FragmentNumber);
        ptr+=BytesToSend;
    }

Then, inside your SendUDPFragment code, you'll put your own wrapper around the data block, such as this:
    struct FragmentBlock
   {
        LONG MessageId;
        LONG FragmentNumber;
        WORD BytesInThisFragment;
        BYTE  LastFragmentFlag;
        BYTE FragmentData[MaximumFragmentSize];
    }

You'd have to set the MessageId based on the computer, process, or some other unique attribute, so that the receiver can receive multiple messages at the same time.  Then, the FragmentNumber can be used to reassemble fragments in the proper order, and the LastFragmentFlag can indicate that this is all the data that's coming.  You'd want to pick a "safe" value for the MaximumFragmentSize, like 500 or so, so that it would work on ANY network.  Again, TCP does this all for you.
   
2) You may need to post multiple listener threads, depending on the frequency of incoming packets.  One I/O system I work with posts (I think) 4 threads when it listens, just to make sure that a sudden burst of activity won't cause something to be missed.  This would be especially important for UDP, which has no built-in retransmission capability.
Accepted Solution
 
 
20080236-EE-VQP-29 / EE_QW_2_20070628