Return-Path: william@bourbon.usc.edu Delivery-Date: Sun Oct 5 16:18:02 2008 X-Spam-Checker-Version: SpamAssassin 3.2.3 (2007-08-08) on merlot.usc.edu X-Spam-Level: X-Spam-Status: No, score=-2.3 required=5.0 tests=AWL,BAYES_00 autolearn=ham version=3.2.3 Received: from bourbon.usc.edu (bourbon.usc.edu [128.125.9.75]) by merlot.usc.edu (8.14.1/8.14.1) with ESMTP id m95NI2E8013250 for ; Sun, 5 Oct 2008 16:18:02 -0700 Received: from bourbon.usc.edu (localhost.localdomain [127.0.0.1]) by bourbon.usc.edu (8.14.2/8.14.1) with ESMTP id m95NNeuN007684 for ; Sun, 5 Oct 2008 16:23:40 -0700 Message-Id: <200810052323.m95NNeuN007684@bourbon.usc.edu> To: cs551@merlot.usc.edu Subject: Re: Regarding connection establishment Date: Sun, 05 Oct 2008 16:23:40 -0700 From: Bill Cheng Someone wrote: > We are trying to implement the initial TCP connection between 2 beacon nodes. > When 2 threads are invoked, both of them run through the client and > server side of the code. > Initially, a thread comes up and tries to "connect" (like a client), > and the connection is refused 'coz no one is there to "listen". > Then this same thread tries to do a "bind" and then a "listen". Should > these be done on the same sockfd as the "connect" ? When we tried > using the same socket, we got a Bind Error. Your server thread (the one that calls bind() and accept()) needs to be a separate thread from your client threads (the one that calls connect()). These client threads here are the ones where you *initiate* connections to its neighbors. Let's call this Type One client thread. There is a 2nd type of client threads. These are the ones where another node initiates connection to this ndoe. After accept() returns, your server thread would create a child thread to handle the connection. Let's call this Type Two client thread. > And would the second thread also be using the same socket as its mentioned that: > "For the purpose of this project, you must use the same socket for > the connection from A to B and from B to A" > > Could you please clarify this ? If node A is connected to ndoe B, from node A's point of view, it should be using either a Type One client thread or a Type Two client thread, *but not both*. For either type, node A will be using the same socket for both reading and writing. You need to think back... When you wrote your client code for warmup project #1, your client first write to the socket and then it reads from the same socket. Similarly, your server forks off a child to handle the connection. The child process first read the socket and the write to the same socket. So, on either side, there is only one socket. (The master socket held by the server is *not* the one that's used for reading and writing.) -- Bill Cheng // bill.cheng@usc.edu