I want to make a chat room using raw socket in C. I have following problems:

Q 1 : Can I use select function to handle multiple connections in case of raw sockets ?

Q 2 : Port nos in sockets are real ports or logically implemented for various applications on transport layer??

Q 3 : I am having one computer only so using lo ( local loop) as my interface. So the process which is initiating the chat has send first and then receive call, so it's receiving it's own data. How to restrict it?

Any help would be appreciated since that would help me in increasing my confidence on raw sockets.

Thanks :)

有帮助吗?

解决方案

If you want this to be a real, usable chat system, stop. Don't use raw sockets. Huge mistake.

If you are just playing around because you want to put “raw sockets” under the “Experience” section of your résumé, you may read on.

  1. You can use the select function to detect when a raw socket has a packet available to receive, and when it can accept a packet to transmit. You can pass multiple file descriptors to a single call to select if you want to check multiple raw sockets (or whatever) simultaneously.

  2. Port numbers are part of the TCP and UDP protocols (and some other transport layer protocols). The kernel doesn't look for port numbers when receiving packets for raw sockets.

  3. The raw(7) man page‚ states:

    All packets or errors matching the protocol number specified for the raw socket are passed to this socket.

    And it also states:

    A raw socket can be bound to a specific local address using the bind(2) call. If it isn't bound, all packets with the specified IP protocol are received.

    Therefore you probably want to at least use different IP addresses for each end of the “connection”, and bind each end to its address.

    “But!” you say, “I'm using loopback! I can only use the 127.0.0.1 address!” Not so, my friend. The entire 127.0.0.0/8 address block is reserved for loopback addresses; 127.0.0.1 is merely the most commonly-used loopback address. Linux (but perhaps not other systems) responds to every address in the loopback block. Try this in one window:

    nc -v -l 10150 
    

    And then in another window:

    nc -s 127.0.0.1 127.0.0.2 10150
    

    You will see that you have created a TCP connection from 127.0.0.1 to 127.0.0.2. I think you can also bind your raw sockets to separate addresses. Then, when you receive a packet, you can check whether it's from the other end's IP address to decide whether to process or discard it.

其他提示

Just curious, why do you want to use raw sockets? Raw sockets (AF_INET, SOCK_RAW) allow you to send out "raw" packets, where you are responsible for crafting everything but the MAC and IP layers.

A1: There are no "connections" with raw sockets. Just packets.

A2: There are no "ports" with raw sockets. Just packets. "Port numbers" as we know them are part of the TCP or UDP protocols, both of which are above the level at which we work with raw sockets.

A3: This is not specific to raw sockets - you would have this issue regardless of your protocol selection. To really answer this, we would need to know much more about your proposed protocol, since right now, you're simply blasting out raw IP packets.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top