?
Solved

figure out is socket is TCP or UDP?

Posted on 2004-08-23
2
Medium Priority
?
296 Views
Last Modified: 2012-05-05
This is what I'm wondering.  Is there a way to fill out the following function?

int IsSocketTcp(SOCKET s)
{
  // Figure out if the socket is TCP or UDP and return appropriate value
}

The platform is windows.
0
Comment
Question by:lwinkenb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 19

Accepted Solution

by:
drichards earned 500 total points
ID: 11878410
Something like this...

bool IsSocketTcp(SOCKET s)
{
    WSAPROTOCOL_INFO info;
    int len = sizeof(info);
    ::getsockopt(s, SOL_SOCKET, SO_PROTOCOL_INFO, (char*)&info, &len);
    // Check info.iSocketType: 1 = SOCK_STREAM (TCP), 2 = SOCK_DGRAM (UDP)
    // Check info.iProtocol: 6 = IPPROTO_TCP, 17 = IPPROTO_UDP
    // See Winsock2.h for other values
    return (info.iSocketType == SOCK_STREAM) && (info.iProtocol == IPPROTO_TCP);
}

I'm not sure IPPROTO_TCP is guaranteed to be set.
0
 
LVL 22

Expert Comment

by:grg99
ID: 11879999
Here's a semi-silly way:  try sending a packet using TCP to say the loopback address.  If it goes out okay, it's a TCP socket.  If you get back the error code "wrong protocol", it is probably UDP.

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Suggested Courses
Course of the Month9 days, 9 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question