Solved

figure out is socket is TCP or UDP?

Posted on 2004-08-23
2
294 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 125 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

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

691 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