We’re rewarding the question askers & reputations are being recalculated! Read more.

Hot answers tagged

117

S is a server program: let's say it's an HTTP server, so it'll use the well-known port number for HTTP, which is 80. I run it on a host with IP address 10.0.0.4, so it will listen for connections on 10.0.0.4:80 (because that's where everyone will expect to find it). Inside S, I'm going to create a socket and bind it to that address: now, the OS knows that ...


64

A socket identifies a connection. Cookies are usually used to identify a user. If I open two browser tabs to SE.SE, I will have two connections and thus two sockets. But I want my settings to persist across both of them. (In fact, typically, a browser opens multiple sockets for one page in order to speed up page load time; I believe most browsers have a ...


50

Think of your machine as an apartment building: A port is an apartment number. A socket is the door of an apartment. An IP address is the street address of the building.


45

A port is part of the address in the TCP and UDP protocols. It is used to help the OS identify which application should get the data that is received. An OS has to support ports to support TCP and UDP because ports are an intrinsic part of TCP and UDP. A socket is part of the interface the OS presents to applications to allow them to send and receive ...


26

Socket programming (at least as the term is normally used) is programming to one specific network API. Sockets support IP-based protocols (primarily TCP and UDP)1. Network programming can be done using various other APIs. Windows has a number of protocol-independent APIs such as the WNet* and Net* functions. Older versions of Windows also used NetBIOS/...


23

An open TCP connection is a logical state. It does not imply that that data is always being sent back and forth. After the initial three-way handshake you've entered into the "connected" state. You're in that state until either a 3-way disconnect occurs or a keep-alive fails. During the lifetime of the connection, resources from the underlying "physical" ...


21

Unix sockets are a bidirectional socket - just like an IP based socket, which you are probably familiar with, and kind of similar to a pipe, which you are probably familiar with. They have a small set of interesting properties: They are in the domain of "the local host" only - you can't access them over the network, only on the local machine. You can ...


20

TCP sockets are designed to be stateful so in general they are used to identify sessions. Protocols like SSH and ftp do exactly this. HTTP is designed to be stateless and each connection is only associated with a resource to be downloaded. After a resource is downloaded the TCP socket that the HTTP request rides on is closed. The original reason for this ...


15

Let me give you an example: Say you want to communicate/chat with your friend, who lives not at your address. For that to happen, you have to establish a "communication channel". Say, you want to do this communication using telephones. You know that there is a network of telephone lines in the city that is extended to every house. Now, there is a telephone ...


15

Berkeley socket was just an API that was invented then. The technology that sockets abstracts away was there before; ARPANET and TCP/IP stack was invented in the 70's and there were computer networks even before that. Technically you can connect two computers with a serial cable an make them communicate over it.


12

C has no native support for serializing structures, so you're on your own. The first order approximation is (as stated in other replies) to define it for primitive types, and apply it recursively to larger structures. However, there are lots of devilish details that have to be addressed beyond the simple concept. To name a few: endian order of integers, ...


12

UDP packets are not guaranteed to arrive in order. You should use TCP for this.


11

A few advantages for each off the top of my head. Note that some of these items may not apply in all cases; these are just general observations. Sockets Simple and controlled. Can be extended to network sockets as necessary with little or no modification. Programming model requires serialization, which in turn requires you to think about what data actually ...


9

What's wrong with plain old garden variety FTP (File Transfer Protocol), or even TFTP (Trivial File Transfer Protocol)? Granted, they're older than your father's memory of his first kiss, but they still work. Now, both FTP and TFTP want the file to be on the server before they serve it to download clients. If what you want is to stream the file from A ...


9

Network administrators like to know exactly what ports your software will listen on, so that they can open only the required ports on the firewall defending their network from intrusion attacks. Protecting a network is all about knowing where an attack could come from. If your network, via its firewall, will never expect traffic on port 699, then if you ...


9

It isn't necessarily bad design, but in an application with lots of connections it will increase your thread consumption (possibly to the point where you can't create more threads). To support large numbers of sockets with small numbers of threads, you would turn to asynchnous IO, using the select(2) system call. If you're only expecting a few connections, ...


8

A computer has an IP address that identifies it as a separate entity on the network. We add an additional number to that to allow us to differentiate between connections to that computer. This is the port number. On the OS side of the connection you need buffers, connection state, etc. This logical object is the socket.


8

The problem is that the client will often be behind a NAT/firewall, which means that the server can't initiate a TCP connection to the client, because the client doesn't have a globally routable IP address and/or because the firewall will block the unexpected incoming TCP packets coming from the server. So that means that any TCP connection will have to be ...


7

Have a look at the work Google has done with Protocol Buffers. You write a .proto file like this: message Person { required int32 id = 1; required string name = 2; optional string email = 3; } Then you compile it with protoc, the protocol buffer compiler, to produce code in C++, Java, or Python. Then, if you are using C++, you use that code like ...


7

I think I understand the question you're asking. Is it: I have a client-server system in which the client can have multiple requests pending simultaneously. When the client receives a response, how can it tell which request corresponds to this response? If so, there are several ways to proceed. Note that in the following discussion we distinguish between ...


6

"network programming" will require some networking technology - for example, RPC. Sockets (most likely you mean BSD sockets) are an example of such technology. So "socket programming" is a subset of "network programming".


6

Sockets are one-to-one. You need multiple sockets if you want to send the same thing to multiple processes. With shared memory, you can have multiple readers, and also multiple writers. Sockets are resource intensive. Each and every message goes through the OS. With shared memory, you map the shared memory but once into your application's memory and from ...


6

If you need to handshake at the application layer (for instance, in order to get a confirmation from the other end that some persistent data has been committed), do that using your CLOSE and CLOSE_OK protocol messages. Indeed you need to do this at the application layer (see the end-to-end principle). But after both ends of the socket are already in ...


6

I think you're looking for a protocol: something that can handle errors, retransmissions, etc. For example, what happens if one of the socket is dropped because the underlying network had a problem? Or if your messages are received twice because of a faulty switch along the line? Or if they arrive in the wrong order? Given that you're planning for a large ...


5

Yes it is possible. This could be hardware and driver dependent, and might be very different depending on what other types of packets are being sent over the line at the same time. How are internal packet buffers handling the incoming send requests? You can't know. It could be using parallel buffers that fill up in odd ways. Since the spec doesn't have a ...


5

I've created a few low level networking programs in c#, mainly a protocol stack, and a chat messenger. The only issue I ever had was the overhead that comes along with using a high level language. You can still do all of the low level things like looking at packets, and reading individual bytes. Java & .NET both have built-in support for sockets and byte ...


5

The standard approach is to divide your world up into large squares/cubes and keep track of which each player is in. You can then iterate over the players in the square the player is in, rather than all the players in the world. The problem comes near the edges of the square. Here you need to check adjacent squares. There are some clever tricks where you ...


4

600 persistent connections should not be an issue - in general connecting overhead is high compared to idle connection overhead(especially if using secure, handshake-based protocol such as TLS), so keeping connections open rather than closing and opening them upon actions definitely reduces the amount of load on the server(and the network, for that matter). ...


Only top voted, non community-wiki answers of a minimum length are eligible