Solved

figure out is socket is TCP or UDP?

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
My eclipse editor won't start ? 4 161
cURL: stopping a http transaction before it's finished 3 126
C Language combined operators 28 114
Arduino EDI - Programming language 3 103
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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
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 while-loops in the C programming language.

808 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