Solved

figure out is socket is TCP or UDP?

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

Technology Partners: 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

Suggested Solutions

Title # Comments Views Activity
How to issue a linefeed, pause, and clear screen in UNIX enviroment 3 77
C language IDE – Compilers installation 14 78
Problem to save 10 174
C Programming - If Statement 8 83
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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

749 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