Solved

figure out is socket is TCP or UDP?

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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 for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

707 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now