Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

shared socket problem

8 views
Skip to first unread message

Shiri Latner - Israel

unread,
Feb 28, 2001, 6:51:28 AM2/28/01
to
Hi,
I have a client and server tasks communicating through stream sockets.
I added a third task which is supposed to share the socket returned by
‘accept’ in the server task.
However, I’m having trouble using the PNA shr_socket command, it fails.
If any of you have any idea why, please let me know
Or just send me a description of how to use the shr_socket command
Thanks
Shiri

--
Posted from [199.203.209.2]
via Mailgate.ORG Server - http://www.Mailgate.ORG

Alex Estrin

unread,
Mar 1, 2001, 2:28:26 PM3/1/01
to
From Manual:
pSOSystem System Concepts

4.4.2 Socket creation
"....
The returned socket descriptor can only be used by the socket’s creator.
However,
the shr_socket() system call can be used to allow other tasks to reference
the
socket:
ns = shr_socket (s, tid);
The parameter s is a socket descriptor used by the calling task to reference
an existing
socket [s is normally a socket descriptor returned by socket()]. The
parameter
tid is the task ID of another task that wants to access the same socket.
shr_socket() returns a new socket descriptor ns, which can be used by tid to
reference the socket. This system call is useful when designing UNIX-style
server
programs."

Hope it will help.

all the best,
Alex

"Shiri Latner - Israel" <Shi...@gilat.com> wrote in message
news:C98B3A58B26A654ABB8DE99D60ABA6B72D160B@gexb...

Christopher Davidson-Rose

unread,
Mar 4, 2001, 7:40:02 AM3/4/01
to
Hi,

One other issue that might be important.  When closing shared sockets, you must remember to close both the shared and original sockets.  Failure to do this results in PSOS keeping a socket descriptor open and may result in your application running out of socket descriptors at some point.  It is good practice (if possible) to close the original socket immediately after creating the shared descriptor.

Thanks

Chris

Alex Estrin wrote:

From Manual:
pSOSystem System Concepts

4.4.2 Socket creation
"....
The returned socket descriptor can only be used by the socket?s creator.

However,
the shr_socket() system call can be used to allow other tasks to reference
the
socket:
ns = shr_socket (s, tid);
The parameter s is a socket descriptor used by the calling task to reference
an existing
socket [s is normally a socket descriptor returned by socket()]. The
parameter
tid is the task ID of another task that wants to access the same socket.
shr_socket() returns a new socket descriptor ns, which can be used by tid to
reference the socket. This system call is useful when designing UNIX-style
server
programs."

Hope it will help.

all the best,
Alex

"Shiri Latner - Israel" <Shi...@gilat.com> wrote in message
news:C98B3A58B26A654ABB8DE99D60ABA6B72D160B@gexb...
Hi,
I have a client and server tasks communicating through stream sockets.
I added a third task which is supposed to share the socket returned by

?accept? in the server task.
However, I?m having trouble using the PNA shr_socket command, it fails.

If any of you have any idea why,  please let me know
Or just send me a description of how to use the shr_socket command
Thanks
Shiri

--
Posted from [199.203.209.2]
via Mailgate.ORG Server - http://www.Mailgate.ORG

-- 
Chris Davidson-Rose - Spider Software
0131 475 7000
"Assassins!"
- Arturo Toscanini (1867-1957) to his orchestra
 

Alex Vinokur

unread,
Mar 6, 2001, 1:54:30 AM3/6/01
to

Shiri Latner - Israel wrote:

> Hi,
> I have a client and server tasks communicating through stream sockets.
> I added a third task which is supposed to share the socket returned by

> ‘accept’ in the server task.
> However, I’m having trouble using the PNA shr_socket command, it fails.


> If any of you have any idea why, please let me know
> Or just send me a description of how to use the shr_socket command
> Thanks
> Shiri
>
> --
> Posted from [199.203.209.2]
> via Mailgate.ORG Server - http://www.Mailgate.ORG

Hi,

Here is a code and another things.

Thanks in advance,
Alex Vinokur

########################################################################
=======================
Stream Socket
Server - (many) Clients
=======================

Part#1. Quotes from pSOS Documentation.

Part#2. Comments and Questions.

Part#3. Enviroment.

Part#4. C-program.

Part#5.1. Running when SHARE_NAMED_SOCKET defined :
---> the *named* socket descriptor to be shared
in the t_server_f1() function (server's task#1);
the shared named socket descriptor will be used
in the send () system call
in the t_server_f2() function (server's task#2);
---> the *accept* socket descriptor to be shared
in the t_server_f1() function (server's task#1);
the shared accept socket descriptor will be used
in the recv () system call
in the t_server_f2() function (server's task#2).

Part#5.2. Running when SHARE_NAMED_SOCKET undefined :
---> only the *accept* socket descriptor to be shared
in the t_server_f1() function (server's task#1);
the shared accept socket descriptor will be used
in the send () and recv () system calls
in the t_server_f2() function (server's task#2).

########################################################################

##################################
### Part#1 : BEGIN ###############
### Quotes from pSOS Documentation
##################################

=======================================
>From the pSOS documentation
-------------------------------------
pRISM+ for pSOSystem/PowerPC
pRISM+ 1.2.3 • pSOSystem 2.2.2

pSOSystem System Calls

pNA+ System Calls
-------------------------------------

[ recv : BEGIN ]
<quote>
-----------------------------------
recv Receives data from a socket.
-----------------------------------

#include <pna.h>
long recv(
int s, // socket descriptor
char *buf, // packet
int len, // packet length
int flags // packet attributes
)


Description

The recv() system call is used to receive data from the specified socket.
The
behavior of this system call depends on the socket type, as described
under
“Arguments.”

The recv() system call returns the number of bytes received, and this
value should
always be checked because this is the only way to detect the actual
number of data
bytes stored in the user buffer.

Applications can use this call to receive messages from the pNA+ network
manager
in a linked list of mblks (message blocks) by setting the MSG_RAWMEM
flag. Using
mblks eliminates the data copy performed in the pNA+ network manager
during the
data transfer between the application and the pNA+ network manager.


Arguments

s Specifies the socket from which data is received. s can be a stream,
a datagram, or a raw socket.

If s is a stream socket, recv() copies whatever data is available at
the socket to the user buffer and returns. recv() never copies more
than len bytes of data to the user buffer, but it can copy less, if less
than len bytes are available. Unless ioctl() was used to mark the
socket non-blocking, recv() blocks the caller if no data is available
at the socket. The caller is unblocked when data is received. If the
socket has been marked non-blocking, recv() returns immediately
whether or not data is received.

</quote>
[ recv : END ]

[ send : BEGIN ]
<quote>
------------------------------
send Sends data to a socket.
------------------------------

#include <pna.h>
long send(
int s, // socket descriptor
char *buf, // packet
int len, // packet length
int flags // packet attributes
)


Description

The send() system call is used to send data to a foreign socket.

If no buffer space is available at the socket to hold the data to be
transmitted,
send() blocks the calling task unless the socket has been marked
non-blocking.

Applications can use this call to pass messages to the pNA+ network
manager in a
linked list of mblks (message blocks) by setting the MSG_RAWMEM flag (see

“Arguments,” below). Using mblks eliminates the data copy performed in
the pNA+
network manager during the data transfer between the application and the
pNA+
network manager.


Arguments

s Specifies the local socket, which must be in a connected state.

If s is a stream socket, the data is sent to the foreign socket that is
connected to s.

</quote>
[ recv : END ]
=======================================

----------------------------------
### Quotes from pSOS Documentation
### Part#1 : END #################
----------------------------------

##################################
### Part#2 : BEGIN ###############
### Comments and Questions #######
##################################

[See the t_server_f1() and t_server_f2() functions]

The program works if the server's task#2 uses
the same shared accept socket descriptor
in the recv() and() send() system calls
(see Part#5.2).


The program doesn't work if the server's task#2 uses
the shared named socket descriptor
in the send() system call
and
the shared accept socket descriptor
in the recv() system call
(see Part#5.1).

Question#1. Is there any situation
that a similar program works
and socket descriptors
in the recv() and send() system calls
are different?

Question#2. [See Part#5.2] Different the server's tasks#2
are created with the same name "ST02".
Is that legal?
Do we have to use different names for tasks#2?

----------------------------------
### Comment and Questions ########
### Part#2 : END #################
----------------------------------

##################################
### Part#3 : BEGIN ###############
### Enviroment ###################
##################################

pRISM+ for pSOSystem/PowerPC
pRISM+ 1.2.3 . pSOSystem 2.2.2
Diab Data D-CC Optimizing C compiler
Windows98

----------------------------------
### Enviroment ###################
### Part#3 : END #################
----------------------------------

##################################
### Part#4 : BEGIN ###############
### C-program ####################
##################################

// =============================================
// === Stream Socket : Server - many Clients ===
// === 1. To invoke server : utest s =========
// === 2. To invoke client#n : utest c <n> =====
// =============================================

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <psos.h>
#include <pna.h>


// ========== typedefs =================
typedef unsigned long ULONG;
typedef unsigned int UINT;
typedef unsigned char UCHAR;
typedef int SOCKET;


// ========== variables ================
ULONG sem4_debug;
ULONG count_print_msg;

char utest_man [] = "utest\n" ;


// ========== common defines ============
#define ASSERT_FLAG
#define DPRINT_FLAG
// #define SHARE_NAMED_SOCKET // ATTENTION (#1)

// --------------------------------------
#define SM_GET(x) rc_sem = sm_p (x, SM_WAIT, 0); \
assert ((!rc_sem) && "sm_p")
#define SM_REL(x) rc_sem = sm_v (x); \
assert ((!rc_sem) && "sm_v")
#define PRINT_L(x) (printf ("#%6d [%s, %4d; %-12s, #%6lX]\t ", \
(++count_print_msg), \
__FILE__, \
__LINE__, \
funcName, \
tidNo), \
printf x)

#define PRINT_SM(x, y) SM_GET(x); PRINT_L(y); SM_REL(x)
// --------------------------------------
#ifdef ASSERT_FLAG
#define ASSERT(x) assert (x)
#else
#define ASSERT(x)
#endif

// --------------------------------------
#ifdef DPRINT_FLAG
#define DL_PRINT(x) PRINT_L(x)
#define DSM_PRINT(x) PRINT_SM(sem4_debug, x)
#else
#define DL_PRINT(x)
#define DSM_PRINT(x)
#endif


// --------------------------------------
#define K_FATAL(x) k_fatal (0x20000000 + x*0x1000 + __LINE__, 0) // __LINE
must be < 0x1000

// ========== special defines ===========
#define SERVER_STREAM_PORT 1400
#define LOOPBACK 0x7F000001 // 127.0.0.1


// ========== structure ================
struct task_param
{
// ===== Init Param =====
char name [4];
ULONG prio;
ULONG sstack;
ULONG ustack;
ULONG flags;
ULONG mode;
void (*tfunc)(ULONG arg0, ULONG arg1, ULONG arg2, ULONG arg3);
ULONG targs [4];

// ====== Result Param =====
ULONG tid;
};


// ========== functions declaration =====
ULONG task_create (struct task_param *ptr_tspm);
ULONG task_start (struct task_param *ptr_tspm);
ULONG task_do_cs (struct task_param *ptr_tspm);

void t_server_f1 (ULONG arg0, ULONG arg1, ULONG arg2, ULONG arg3);
void t_server_f2 (ULONG arg0, ULONG arg1, ULONG arg2, ULONG arg3);
void t_client_f (ULONG arg0, ULONG arg1, ULONG arg2, ULONG arg3);

// ########################################

// =================================================
// ===== Function : task_create ====================
// ----- Argument : pointer to struct task_param ---
// ----- Return : t_create's return value --------
// =================================================
ULONG task_create (struct task_param *ptr_tspm)
{
const char funcName [] = "task_create";
ULONG tidNo;
ULONG rc_create;
ULONG rc_sem;
ULONG ret_ULONG_code;

// -------------------------
tidNo = 0;
ret_ULONG_code = t_ident (NULL, 0, &tidNo);
ASSERT (!ret_ULONG_code);
// -------------------------


rc_create = t_create (
ptr_tspm->name,
ptr_tspm->prio,
ptr_tspm->sstack,
ptr_tspm->ustack,
ptr_tspm->flags,
&(ptr_tspm->tid)
);

switch (rc_create)
{
case 0 :
// Empty : Success
break;

case ERR_OBJTFULL :
case ERR_NOTCB :
case ERR_NOSTK :
case ERR_TINYSTK :
case ERR_PRIOR :
DSM_PRINT (("Unable to create task <%s>, tid#%lX (hex); ERROR %lX
(hex)\n",
ptr_tspm->name,
ptr_tspm->tid,
rc_create
));
return rc_create;
break; // unused


default :
DSM_PRINT (("Unable to create task <%s>, tid#%lX (hex); ILLEGAL ERROR
%lX (hex)\n",
ptr_tspm->name,
ptr_tspm->tid,
rc_create
));

ASSERT (0);
return rc_create;
break; // unused
} // end of switch

// ------------------
ASSERT (!rc_create);
// ------------------

DSM_PRINT (("Task <%s>, tid#%lX (hex) created\n",
ptr_tspm->name,
ptr_tspm->tid
));

return rc_create;
} // task_create


// =================================================
// ===== Function : task_start =====================
// ----- Argument : pointer to struct task_param ---
// ----- Return : t_start's return value ---------
// =================================================
ULONG task_start (struct task_param *ptr_tspm)
{
const char funcName [] = "task_start";
ULONG tidNo;
ULONG rc_start;
ULONG rc_sem;
ULONG ret_ULONG_code;

// -------------------------
tidNo = 0;
ret_ULONG_code = t_ident (NULL, 0, &tidNo);
ASSERT (!ret_ULONG_code);
// -------------------------


rc_start = t_start (
ptr_tspm->tid,
ptr_tspm->mode,
ptr_tspm->tfunc,
ptr_tspm->targs
);

switch (rc_start)
{
case 0 :
// Empty : Success
break;

case ERR_OBJDEL :
case ERR_OBJID :
case ERR_OBJTYPE :
case ERR_ACTIVE :
case ERR_ILLRSC :
case ERR_NOSTK :
DSM_PRINT (("Unable to start task tid#%lX (hex); ERROR %lX (hex)\n",
ptr_tspm->tid,
rc_start
));


return rc_start;
break; // unused

default :
DSM_PRINT (("Unable to start task tid#%lX (hex); ILLEGAL ERROR %lX
(hex)\n",
ptr_tspm->tid,
rc_start
));
ASSERT (0);

return rc_start;
break; // unused
} // end of switch

// ------------------
ASSERT (!rc_start);
// ------------------

DSM_PRINT (("Task tid#%lX (hex) started\n", ptr_tspm->tid));

return rc_start;
} // task_start


// =================================================
// ===== Function : task_do_cs =====================
// ----- Argument : pointer to struct task_param ---
// ----- Return : t_create or t_start's ret_value
// =================================================
ULONG task_do_cs (struct task_param *ptr_tspm)
{
const char funcName [] = "task_do_cs";
ULONG tidNo;

ULONG rc_value;
ULONG rc_sem;
ULONG ret_ULONG_code;

// -------------------------
tidNo = 0;
ret_ULONG_code = t_ident (NULL, 0, &tidNo);
ASSERT (!ret_ULONG_code);
// -------------------------


rc_value = task_create (ptr_tspm);

if (!rc_value)
{
rc_value = task_start (ptr_tspm);
}
return rc_value;
} // task_do_cs


// =================================================
// ===== Function : utest ========================
// ----- Invocation : ------------------------------
// ----- utest s : invokes a server -----------
// ----- utest c <n> : invokes a client#n ---------
// =================================================
void utest (int argc, char **argv, char **env)
{
const char funcName [] = "utest";
ULONG tidNo;

struct task_param ts_param;

ULONG ret_ULONG_code;
ULONG rc_sem;

ULONG i;
int client_no;

char str_print [1000];

#define SEM1_NAME "SEM1"

// -------------------------
tidNo = 0;
ret_ULONG_code = t_ident (NULL, 0, &tidNo);
ASSERT (!ret_ULONG_code);
// -------------------------


strcpy (str_print, "");
for (i = 0; i < argc; i++)
{
strcat (str_print, argv[i]);
if (i < (argc - 1))
{
strcat (str_print, " ");
}
}

count_print_msg = 0;

ret_ULONG_code = sm_create (SEM1_NAME, 1, 0, &sem4_debug);

switch (ret_ULONG_code)
{
case 0 :
// Empty : success
break;

case ERR_OBJTFULL :
case ERR_NOSCB :
// Here SM_GET and SM_REL are not used
DL_PRINT (("Unable to create semaphore <%s>; ERROR %lX (hex)\n",
SEM1_NAME,
ret_ULONG_code
));

return;
break; // unused

default :
// Here SM_GET and SM_REL are not used
DL_PRINT (("Unable to create semaphore <%s>; ILLEGAL ERROR %lX (hex)\n",

SEM1_NAME,
ret_ULONG_code
));
ASSERT (0);
return;
break; // unused
} // end of switch

// ==================
ASSERT (!ret_ULONG_code);


// ==================
if (!(argc >= 2))
{
DSM_PRINT (("Illegal command line; argc must be >= 2; real command line
is <%s>\n", str_print));
return;
}
ASSERT (argc >= 2);

// ==================
if (!(strlen (argv [1]) == 1))
{
DSM_PRINT (("argv[1] must be char; real argv[1] = <%s>\n", argv [1]));
return;
}
ASSERT (strlen (argv[1]) == 1);


switch (argv[1][0])
{
case 's' :
// Create server
// ========== server_stream param =============
strcpy (ts_param.name, "ST01");
ts_param.prio = 235;
ts_param.sstack = 1024;
ts_param.ustack = 1024;
ts_param.flags = 0;

ts_param.mode = T_NOPREEMPT | T_TSLICE;
ts_param.tfunc = t_server_f1;
// =====================================

break;

case 'c' :
// Create client #n
// ==================
if (!(argc >= 3))
{
DSM_PRINT (("Illegal command line; argc must be >= 3; real command line
is <%s>\n", str_print));
return;
}
ASSERT (argc >= 3);


if (!(strlen (argv [2]) == 1))
{
DSM_PRINT (("argv[2] must be char; real argv[2] = <%s>\n", argv [2]));
return;
}
ASSERT (strlen (argv [2]) == 1);

client_no = atoi (argv [2]);
if (client_no == 0)
{
DSM_PRINT (("argv[2] must be digit; Unable to do atoi : %s\n", strerror
(errno)));
return;
}
ASSERT ((client_no >= 1) && (client_no <= 9));

// ========== client_stream param =============
strcpy (ts_param.name, "CT01");
ts_param.prio = 225;
ts_param.sstack = 1024;
ts_param.ustack = 1024;
ts_param.flags = 0;

ts_param.mode = T_NOPREEMPT | T_TSLICE;
ts_param.tfunc = t_client_f;
ts_param.targs[0] = client_no;
// =====================================
break;

default :
DSM_PRINT (("argv[1] must be <s> or <c>; real argv[1] = <%s>\n", argv
[1]));
return;
break; // unused

} // switch (argv[1][0])


// ----------------------
ret_ULONG_code = task_do_cs (&ts_param);

if (ret_ULONG_code)
{
DSM_PRINT (("\t##### FAILURE Task : %s #####\n", str_print));
return;
}


} // utest


// =================================================
// ===== Function : t_server_f1 ====================
// ----- arg0, arg1, arg2, arg3 unused -------------
// =================================================
void t_server_f1 (ULONG arg0, ULONG arg1, ULONG arg2, ULONG arg3)
{
const char funcName [] = "t_server_f1";
ULONG tidNo;

int i_index;

ULONG ret_ULONG_code;
long ret_long_code;
int nbytes;
int addr_len;

SOCKET named_sd;
SOCKET accept_sd;
SOCKET shr_named_sd;
SOCKET shr_accept_sd;
SOCKET recv_sd;
SOCKET send_sd;

ULONG rc_sem;
char srv_t2_name [4];
ULONG srv_t2_tid;

struct sockaddr_in our_addr;
struct sockaddr_in their_addr;
struct task_param tsk_param;

#define MAX_QUEUE_SIZE 9
#define MAX_BUFFLEN 21
char buff [MAX_BUFFLEN];
char str_print [MAX_BUFFLEN + 1];

// -------------------------
tidNo = 0;
ret_ULONG_code = t_ident (NULL, 0, &tidNo);
ASSERT (!ret_ULONG_code);
// -------------------------

//-------------------------------------------------
named_sd = socket(AF_INET, SOCK_STREAM, TCP);
if (named_sd == -1)
{
DSM_PRINT (("socket ERROR : errno = %lX (hex), errtext = %s\n",
errno,
(strerror (errno))
));
K_FATAL (errno);
}
ASSERT (named_sd >= 0);

DSM_PRINT (("Server : named_sd = %d (after socket (AF_INET, SOCK_STREAM,
TCP))\n", named_sd));

our_addr.sin_family = AF_INET;
our_addr.sin_port = htons(SERVER_STREAM_PORT);
our_addr.sin_addr.s_addr = htonl(INADDR_ANY);

for (i_index = 0; i_index < 8; i_index++)
{
our_addr.sin_zero[i_index] = 0;
}

ret_long_code = bind(named_sd, &our_addr, sizeof(our_addr));
if (ret_long_code == -1)
{
DSM_PRINT (("bind ERROR : errno = %lX (hex), errtext = %s\n",
errno,
(strerror (errno))
));
K_FATAL(errno);
}
ASSERT (ret_long_code >= 0);

//-----------------------------------------
ret_long_code = listen(named_sd, MAX_QUEUE_SIZE);
if (ret_long_code == -1)
{
DSM_PRINT (("listen ERROR : errno = %lX (hex), errtext = %s\n",
errno,
(strerror (errno))
));
K_FATAL(errno);
}
ASSERT (ret_long_code >= 0);


while (1)
{
addr_len = sizeof(their_addr);
accept_sd = accept(named_sd, &their_addr, &addr_len);
if (accept_sd == -1)
{
DSM_PRINT (("accept ERROR : errno = %lX (hex), errtext = %s\n",
errno,
(strerror (errno))
));
K_FATAL(errno);
}
ASSERT (accept_sd >= 0);

DSM_PRINT (("accept_sd = %d (after accept (named_sd#%d, &their_addr,
&addr_len))\n",
accept_sd,
named_sd
));

// ========== server_stream param =============
strcpy (srv_t2_name, "ST02");
strcpy (tsk_param.name, srv_t2_name);
tsk_param.prio = 230;
tsk_param.sstack = 1024;
tsk_param.ustack = 1024;
tsk_param.flags = 0;

tsk_param.mode = 0;
tsk_param.tfunc = t_server_f2;
// =====================================

ret_ULONG_code = task_create (&tsk_param);

if (ret_ULONG_code)
{
DSM_PRINT (("\t##### FAILURE task_create : t_server_f2 #####\n"));
t_suspend (0);
}

ret_ULONG_code = t_ident (srv_t2_name, 0, &srv_t2_tid);
switch (ret_ULONG_code)
{
case 0 :
// Empty : Success
break;

case ERR_NODENO :
case ERR_OBJNF :
DSM_PRINT (("Unable to execute t_ident for srv_t2_name = <%s>; ERROR
%lX (hex)\n",
srv_t2_name,
ret_ULONG_code
));
t_suspend (0);
break;

default :
DSM_PRINT (("Unable to execute t_ident for srv_t2_name = <%s>; ILLEGAL
ERROR %lX (hex)\n",
srv_t2_name,
ret_ULONG_code
));
t_suspend (0);
break;
} // switch (ret_ULONG_code)

ASSERT (ret_ULONG_code == 0);

// ----- sharing accept_sd (BEGIN) -----
shr_accept_sd = shr_socket (accept_sd, srv_t2_tid);
if (shr_accept_sd == -1)
{
DSM_PRINT (("shr_socket ERROR : errno = %lX (hex), errtext = %s\n",
errno,
(strerror (errno))
));
K_FATAL(errno);
}
ASSERT (shr_accept_sd >= 0);

DSM_PRINT (("shr_accept_sd = %d (when accept_sd#%d, tid = %lX (hex))\n",
shr_accept_sd,
accept_sd,
srv_t2_tid
));

// ===================================
// =========== ATTENTION (#2) ========
// ===================================
tsk_param.targs [0] = shr_accept_sd;
tsk_param.targs [1] = shr_accept_sd;
// ===================================
// ----- sharing accept_sd (END) -----


// --- sharing shr_named_sd (BEGIN) --

// ===================================
// =========== ATTENTION (#3) ========
// ===================================
#ifdef SHARE_NAMED_SOCKET
shr_named_sd = shr_socket (named_sd, srv_t2_tid);
if (shr_named_sd == -1)
{
DSM_PRINT (("shr_socket ERROR : errno = %lX (hex), errtext = %s\n",
errno,
(strerror (errno))
));
K_FATAL(errno);
}
ASSERT (shr_named_sd >= 0);

DSM_PRINT (("shr_socket = %d (when sd#%d, tid = %lX (hex))\n",
shr_named_sd,
named_sd,
srv_t2_tid
));

tsk_param.targs [1] = shr_named_sd;
#endif
// --- sharing shr_named_sd (END) ----

ret_ULONG_code = task_start (&tsk_param);

if (ret_ULONG_code)
{
DSM_PRINT (("\t##### FAILURE Task : t_server_f2 #####\n"));
t_suspend (0);
}
} // while


t_suspend(0);
}// void t_server_f1


// =================================================
// ===== Function : t_server_f2 ====================
// ----- arg0 : recv socket descriptor ---------
// ----- arg1 : send socket descriptor ---------
// ----- arg2, arg3 unused -------------------------
// =================================================
void t_server_f2 (ULONG recv_sd, ULONG send_sd, ULONG arg2, ULONG arg3)
{
const char funcName [] = "t_server_f2";
ULONG tidNo;

int i_index;
int j_index;

ULONG ret_ULONG_code;
long ret_long_code;
int nbytes;
int addr_len;

ULONG rc_sem;

struct sockaddr_in our_addr;
struct sockaddr_in their_addr;

#define MAX_BUFFLEN 21
char buff [MAX_BUFFLEN];
char str_print [MAX_BUFFLEN + 1];


// ------------------
tidNo = 0;
ret_ULONG_code = t_ident (NULL, 0, &tidNo);
ASSERT (!ret_ULONG_code);
// ------------------

//-----------------------------------------
for (i_index = 1; ; i_index++)
{
DSM_PRINT (("\t---> Server (task#2) : iteration#%ld started\n",
i_index));

nbytes = recv(recv_sd, buff, MAX_BUFFLEN, 0);
if (nbytes == -1)
{
DSM_PRINT (("recv ERROR : errno = %lX (hex), errtext = %s; Note: recv_sd
= %d, Max_BUFFLEN = %d, nbytes = %d\n",
errno,
(strerror (errno)),
recv_sd,
MAX_BUFFLEN,
nbytes
));
K_FATAL(errno);
}
ASSERT (nbytes >= 0);

#ifdef DPRINT_FLAG
// ------- debug calculation -------
for (j_index = 0; j_index < nbytes; j_index++)
{
str_print [j_index] = buff [j_index];
}
str_print [nbytes] = '\0';
// ----------------------------------
#endif
DSM_PRINT (("Server got (via recv_sd#%d) %d bytes : <%s>\n", recv_sd,
nbytes, str_print));

for (j_index = 0; j_index < nbytes; j_index++)
{
if ((buff[j_index] >= 'a') && (buff[j_index] <= 'z'))
{
buff[j_index] = buff[j_index] - 'a' + 'A';
}
}

#ifdef DPRINT_FLAG
// ------- debug calculation -------
for (j_index = 0; j_index < nbytes; j_index++)
{
str_print [j_index] = buff [j_index];
}
str_print [nbytes] = '\0';
// ----------------------------------
#endif
DSM_PRINT (("Server wants to send (via send_sd#%d) %d bytes : <%s>\n",
send_sd, nbytes, str_print));


ret_long_code = send(send_sd, buff, nbytes, 0);
if (ret_long_code == -1)
{
DSM_PRINT (("send ERROR : errno = %lX (hex), errtext = %s; Note: send_sd
= %d, nbytes = %d\n",
errno,
(strerror (errno)),
send_sd,
nbytes
));
K_FATAL(errno);
}
ASSERT (ret_long_code >= 0);

#ifdef DPRINT_FLAG
// ------- debug calculation -------
for (j_index = 0; j_index < nbytes; j_index++)
{
str_print [j_index] = buff [j_index];
}
str_print [nbytes] = '\0';
// ---------------------------------
#endif
DSM_PRINT (("Server sent (via send_sd#%d) %d bytes : <%s>\n", send_sd,
nbytes, str_print));

DSM_PRINT (("\t===> Server (task#2) : iteration#%ld finished\n",
i_index));

} // for (;;)

t_suspend(0);

} // void t_server_f2

// =================================================
// ===== Function : t_client_f ==================
// ----- arg0 : client number -----------------
// ----- arg1, arg2, arg3 unused -------------------
// =================================================
void t_client_f (ULONG client_no, ULONG arg1, ULONG arg2, ULONG arg3)
{
const char funcName [] = "t_client_f";
ULONG tidNo;

UINT i_index;

SOCKET sd;
ULONG ret_ULONG_code;
long ret_long_code;
int nbytes;
int real_bufflen;

ULONG iteration;
ULONG rc_sem;

struct sockaddr_in server_addr;

#define MIN_BUFFLEN 0
#define MAX_BUFFLEN 21
UCHAR buff [MAX_BUFFLEN];
char str_print [MAX_BUFFLEN + 1];


// ------------------
tidNo = 0;
ret_ULONG_code = t_ident (NULL, 0, &tidNo);
ASSERT (!ret_ULONG_code);
// ------------------

DSM_PRINT (("Client#%d begins\n", client_no));

// -------------------------
sd = socket (AF_INET, SOCK_STREAM, TCP);
if (sd == -1)
{
DSM_PRINT (("socket ERROR : errno = %lX (hex), errtext = %s\n",
errno,
(strerror (errno))
));
K_FATAL (errno);
}

DSM_PRINT (("Client#%d : sd = %d (after socket (AF_INET, SOCK_STREAM,
TCP))\n",
client_no,
sd
));

ASSERT (sd > 0);

// -------------------------

server_addr.sin_family = AF_INET;
server_addr.sin_port = htons (SERVER_STREAM_PORT);
server_addr.sin_addr.s_addr = htonl (LOOPBACK);

for (i_index = 0; i_index < 8; i_index++)
{
server_addr.sin_zero [i_index] = 0;
}

ret_long_code = connect (sd, &server_addr, sizeof (server_addr));
if (ret_long_code == -1)
{
DSM_PRINT (("connect ERROR : errno = %lX (hex), errtext = %s\n",
errno,
(strerror (errno))
));
K_FATAL (errno);
}
ASSERT (ret_long_code >= 0);

//----------------------------
// Main loop - executes forever
//----------------------------
real_bufflen = MIN_BUFFLEN + (int)client_no;
for (iteration = 1; ; iteration++)
{
DSM_PRINT(("\t-----> Client#%d : iteration#%ld started\n", client_no,
iteration));

ASSERT (real_bufflen < MAX_BUFFLEN);
for (i_index = 0; i_index < real_bufflen; i_index++)
{
buff[i_index] = 'a' + i_index;
}

//----------------------------
// Send the data to the server.
//----------------------------
ret_long_code = send(sd, (char *)buff, real_bufflen, 0);
if (ret_long_code == -1)
{
DSM_PRINT (("send ERROR : errno = %lX (hex), errtext = %s\n",
errno,
(strerror (errno))
));
K_FATAL(errno);
}
ASSERT (ret_long_code >= 0);

if (ret_long_code != real_bufflen)
{
DSM_PRINT (("send ERROR : errno = %lX (hex), errtext = %s\n",
errno,
(strerror (errno))
));
K_FATAL(ret_long_code);

}
ASSERT (ret_long_code == real_bufflen);

#ifdef DPRINT_FLAG
// ------- debug calculation -------
for (i_index = 0; i_index < ret_long_code; i_index++)
{
str_print [i_index] = buff [i_index];
}
str_print [ret_long_code] = '\0';
// ---------------------------------
#endif
DSM_PRINT (("Client#%d sent (via sd#%d) %d bytes : <%s>\n",
client_no,
sd,
ret_long_code,
str_print
));


//-------------------------------------
// Get the transformed data back from the server.
//-------------------------------------
nbytes = recv(sd, (char *)buff, MAX_BUFFLEN, 0);
if (nbytes == -1)
{
DSM_PRINT (("recv ERROR : errno = %lX (hex), errtext = %s\n",
errno,
(strerror (errno))
));
K_FATAL(errno);
}
ASSERT (nbytes >= 0);

#ifdef DPRINT_FLAG
// ------- debug calculation -------
for (i_index = 0; i_index < nbytes; i_index++)
{
str_print [i_index] = buff [i_index];
}
str_print [nbytes] = '\0';
// ---------------------------------
#endif
DSM_PRINT (("Client#%d got (via sd#%d) %d bytes : <%s>\n",
client_no,
sd,
nbytes,
str_print
));

//-------------
tm_wkafter(1000);

DSM_PRINT(("\t=====> Client#%d : iteration#%ld finished\n",
client_no, iteration));

} // for (iteration = 1; ; iteration++)

t_suspend(0);

}// void t_client_f

----------------------------------
### C-program ####################
### Part#4 : END #################
----------------------------------


##################################
### Part#5.1 : BEGIN #############
### Running ######################
### SHARE_NAMED_SOCKET defined ###
##################################


com> utest s

# 1 [utest.c, 175; task_create , #2D0000] Task <ST01>, tid#300000
(hex) created
# 2 [utest.c, 510; t_server_f1 , #300000] Server : named_sd = 1
(after socket (AF_INET, SOCK_STREAM, TCP))
# 3 [utest.c, 247; task_start , #2D0000] Task tid#300000 (hex)
started
com> utest c 3

# 1 [utest.c, 175; task_create , #2D0000] Task <CT01>, tid#320000
(hex) created
# 2 [utest.c, 835; t_client_f , #320000] Client#3 begins
# 3 [utest.c, 247; task_start , #2D0000] Task tid#320000 (hex)
started
# 4 [utest.c, 848; t_client_f , #320000] com> Client#3 : sd = 1
(after socket (AF_INET, SOCK_STREAM, TCP))
# 5 [utest.c, 559; t_server_f1 , #300000] accept_sd = 2 (after
accept (named_sd#1, &their_addr, &addr_len))
# 6 [utest.c, 883; t_client_f , #320000] -----> Client#3 :
iteration#1 started
# 7 [utest.c, 175; task_create , #300000] Task <ST02>, tid#330000
(hex) created
# 8 [utest.c, 925; t_client_f , #320000] Client#3 sent (via sd#1) 3
bytes : <abc>
# 9 [utest.c, 623; t_server_f1 , #300000] shr_accept_sd = 1 (when
accept_sd#2, tid = 330000 (hex))
# 10 [utest.c, 655; t_server_f1 , #300000] shr_socket = 2 (when sd#1,
tid = 330000 (hex))
# 11 [utest.c, 247; task_start , #300000] Task tid#330000 (hex)
started
# 12 [utest.c, 717; t_server_f2 , #330000] ---> Server
(task#2) : iteration#1 started
# 13 [utest.c, 742; t_server_f2 , #330000] Server got (via recv_sd#1)
3 bytes : <abc>
# 14 [utest.c, 763; t_server_f2 , #330000] Server wants to send (via
send_sd#2) 3 bytes : <ABC>
# 15 [utest.c, 769; t_server_f2 , #330000] send ERROR : errno = 5039
(hex), errtext = pNA+ error; Note: send_sd = 2, nbytes = 3
===> Error Code : 0x5039 ENOTCONN The socket is not connected.

----------------------------------
### Running ######################
### SHARE_NAMED_SOCKET defined ###
### Part#5.1 : END ###############
----------------------------------

##################################
### Part#5.2 : BEGIN #############
### Running ######################
### SHARE_NAMED_SOCKET undefined #
##################################


com> utest s

# 1 [utest.c, 175; task_create , #2D0000] Task <ST01>, tid#300000
(hex) created
# 2 [utest.c, 510; t_server_f1 , #300000] Server : named_sd = 1
(after socket (AF_INET, SOCK_STREAM, TCP))
# 3 [utest.c, 247; task_start , #2D0000] Task tid#300000 (hex)
started
com> utest c 3

# 1 [utest.c, 175; task_create , #2D0000] Task <CT01>, tid#320000
(hex) created
# 2 [utest.c, 835; t_client_f , #320000] Client#3 begins
# 3 [utest.c, 247; task_start , #2D0000] Task tid#320000 (hex)
started
# 4 [utest.c, 848; t_client_f , #320000] com> Client#3 : sd = 1
(after socket (AF_INET, SOCK_STREAM, TCP))
# 5 [utest.c, 559; t_server_f1 , #300000] accept_sd = 2 (after
accept (named_sd#1, &their_addr, &addr_len))
# 6 [utest.c, 883; t_client_f , #320000] -----> Client#3 :
iteration#1 started
# 7 [utest.c, 175; task_create , #300000] Task <ST02>, tid#330000
(hex) created
# 8 [utest.c, 925; t_client_f , #320000] Client#3 sent (via sd#1) 3
bytes : <abc>
# 9 [utest.c, 623; t_server_f1 , #300000] shr_accept_sd = 1 (when
accept_sd#2, tid = 330000 (hex))
# 10 [utest.c, 247; task_start , #300000] Task tid#330000 (hex)
started
# 11 [utest.c, 717; t_server_f2 , #330000] ---> Server
(task#2) : iteration#1 started
# 12 [utest.c, 742; t_server_f2 , #330000] Server got (via recv_sd#1)
3 bytes : <abc>
# 13 [utest.c, 763; t_server_f2 , #330000] Server wants to send (via
send_sd#1) 3 bytes : <ABC>
# 14 [utest.c, 788; t_server_f2 , #330000] Server sent (via
send_sd#1) 3 bytes : <ABC>
# 15 [utest.c, 956; t_client_f , #320000] Client#3 got (via sd#1) 3
bytes : <ABC>
# 16 [utest.c, 790; t_server_f2 , #330000] ===> Server
(task#2) : iteration#1 finished
# 17 [utest.c, 717; t_server_f2 , #330000] ---> Server
(task#2) : iteration#2 started
# 18 [utest.c, 968; t_client_f , #320000] =====> Client#3 :
iteration#1 finished
# 19 [utest.c, 883; t_client_f , #320000] -----> Client#3 :
iteration#2 started
# 20 [utest.c, 925; t_client_f , #320000] Client#3 sent (via sd#1) 3
bytes : <abc>
# 21 [utest.c, 742; t_server_f2 , #330000] Server got (via recv_sd#1)
3 bytes : <abc>
# 22 [utest.c, 763; t_server_f2 , #330000] Server wants to send (via
send_sd#1) 3 bytes : <ABC>
# 23 [utest.c, 788; t_server_f2 , #330000] Server sent (via
send_sd#1) 3 bytes : <ABC>
# 24 [utest.c, 956; t_client_f , #320000] Client#3 got (via sd#1) 3
bytes : <ABC>
# 25 [utest.c, 790; t_server_f2 , #330000] ===> Server
(task#2) : iteration#2 finished
# 26 [utest.c, 717; t_server_f2 , #330000] ---> Server
(task#2) : iteration#3 started
# 27 [utest.c, 968; t_client_f , #320000] =====> Client#3 :
iteration#2 finished
# 28 [utest.c, 883; t_client_f , #320000] -----> Client#3 :
iteration#3 started
# 29 [utest.c, 925; t_client_f , #320000] Client#3 sent (via sd#1) 3
bytes : <abc>
# 30 [utest.c, 742; t_server_f2 , #330000] Server got (via recv_sd#1)
3 bytes : <abc>
# 31 [utest.c, 763; t_server_f2 , #330000] Server wants to send (via
send_sd#1) 3 bytes : <ABC>
# 32 [utest.c, 788; t_server_f2 , #330000] Server sent (via
send_sd#1) 3 bytes : <ABC>
# 33 [utest.c, 956; t_client_f , #320000] Client#3 got (via sd#1) 3
bytes : <ABC>
# 34 [utest.c, 790; t_server_f2 , #330000] ===> Server
(task#2) : iteration#3 finished
# 35 [utest.c, 717; t_server_f2 , #330000] ---> Server
(task#2) : iteration#4 started
utest c 5

# 1 [utest.c, 175; task_create , #2D0000] Task <CT01>, tid#350000
(hex) created
# 2 [utest.c, 835; t_client_f , #350000] Client#5 begins
# 3 [utest.c, 247; task_start , #2D0000] Task tid#350000 (hex)
started
# 4 [utest.c, 848; t_client_f , #350000] com> Client#5 : sd = 1
(after socket (AF_INET, SOCK_STREAM, TCP))
# 5 [utest.c, 559; t_server_f1 , #300000] accept_sd = 3 (after
accept (named_sd#1, &their_addr, &addr_len))
# 6 [utest.c, 883; t_client_f , #350000] -----> Client#5 :
iteration#1 started
# 7 [utest.c, 175; task_create , #300000] Task <ST02>, tid#360000
(hex) created
# 8 [utest.c, 925; t_client_f , #350000] Client#5 sent (via sd#1) 5
bytes : <abcde>
# 9 [utest.c, 623; t_server_f1 , #300000] shr_accept_sd = 1 (when
accept_sd#3, tid = 360000 (hex))
# 10 [utest.c, 247; task_start , #300000] Task tid#360000 (hex)
started
# 11 [utest.c, 717; t_server_f2 , #360000] ---> Server
(task#2) : iteration#1 started
# 12 [utest.c, 742; t_server_f2 , #360000] Server got (via recv_sd#1)
5 bytes : <abcde>
# 13 [utest.c, 763; t_server_f2 , #360000] Server wants to send (via
send_sd#1) 5 bytes : <ABCDE>
# 14 [utest.c, 788; t_server_f2 , #360000] Server sent (via
send_sd#1) 5 bytes : <ABCDE>
# 15 [utest.c, 956; t_client_f , #350000] Client#5 got (via sd#1) 5
bytes : <ABCDE>
# 16 [utest.c, 790; t_server_f2 , #360000] ===> Server
(task#2) : iteration#1 finished
# 17 [utest.c, 717; t_server_f2 , #360000] ---> Server
(task#2) : iteration#2 started
# 18 [utest.c, 968; t_client_f , #320000] =====> Client#3 :
iteration#3 finished
# 19 [utest.c, 883; t_client_f , #320000] -----> Client#3 :
iteration#4 started
# 20 [utest.c, 925; t_client_f , #320000] Client#3 sent (via sd#1) 3
bytes : <abc>
# 21 [utest.c, 742; t_server_f2 , #330000] Server got (via recv_sd#1)
3 bytes : <abc>
# 22 [utest.c, 763; t_server_f2 , #330000] Server wants to send (via
send_sd#1) 3 bytes : <ABC>
# 23 [utest.c, 788; t_server_f2 , #330000] Server sent (via
send_sd#1) 3 bytes : <ABC>
# 24 [utest.c, 956; t_client_f , #320000] Client#3 got (via sd#1) 3
bytes : <ABC>
# 25 [utest.c, 790; t_server_f2 , #330000] ===> Server
(task#2) : iteration#4 finished
# 26 [utest.c, 717; t_server_f2 , #330000] ---> Server
(task#2) : iteration#5 started
# 27 [utest.c, 968; t_client_f , #350000] =====> Client#5 :
iteration#1 finished
# 28 [utest.c, 883; t_client_f , #350000] -----> Client#5 :
iteration#2 started
# 29 [utest.c, 925; t_client_f , #350000] Client#5 sent (via sd#1) 5
bytes : <abcde>
# 30 [utest.c, 742; t_server_f2 , #360000] Server got (via recv_sd#1)
5 bytes : <abcde>
# 31 [utest.c, 763; t_server_f2 , #360000] Server wants to send (via
send_sd#1) 5 bytes : <ABCDE>
# 32 [utest.c, 788; t_server_f2 , #360000] Server sent (via
send_sd#1) 5 bytes : <ABCDE>
# 33 [utest.c, 956; t_client_f , #350000] Client#5 got (via sd#1) 5
bytes : <ABCDE>
# 34 [utest.c, 790; t_server_f2 , #360000] ===> Server
(task#2) : iteration#2 finished
# 35 [utest.c, 717; t_server_f2 , #360000] ---> Server
(task#2) : iteration#3 started
# 36 [utest.c, 968; t_client_f , #320000] =====> Client#3 :
iteration#4 finished
# 37 [utest.c, 883; t_client_f , #320000] -----> Client#3 :
iteration#5 started
# 38 [utest.c, 925; t_client_f , #320000] Client#3 sent (via sd#1) 3
bytes : <abc>
# 39 [utest.c, 742; t_server_f2 , #330000] Server got (via recv_sd#1)
3 bytes : <abc>
# 40 [utest.c, 763; t_server_f2 , #330000] Server wants to send (via
send_sd#1) 3 bytes : <ABC>
# 41 [utest.c, 788; t_server_f2 , #330000] Server sent (via
send_sd#1) 3 bytes : <ABC>
# 42 [utest.c, 956; t_client_f , #320000] Client#3 got (via sd#1) 3
bytes : <ABC>
# 43 [utest.c, 790; t_server_f2 , #330000] ===> Server
(task#2) : iteration#5 finished
# 44 [utest.c, 717; t_server_f2 , #330000] ---> Server
(task#2) : iteration#6 started
# 45 [utest.c, 968; t_client_f , #350000] =====> Client#5 :
iteration#2 finished
# 46 [utest.c, 883; t_client_f , #350000] -----> Client#5 :
iteration#3 started
# 47 [utest.c, 925; t_client_f , #350000] Client#5 sent (via sd#1) 5
bytes : <abcde>
# 48 [utest.c, 742; t_server_f2 , #360000] Server got (via recv_sd#1)
5 bytes : <abcde>
# 49 [utest.c, 763; t_server_f2 , #360000] Server wants to send (via
send_sd#1) 5 bytes : <ABCDE>
# 50 [utest.c, 788; t_server_f2 , #360000] Server sent (via
send_sd#1) 5 bytes : <ABCDE>
# 51 [utest.c, 956; t_client_f , #350000] Client#5 got (via sd#1) 5
bytes : <ABCDE>
# 52 [utest.c, 790; t_server_f2 , #360000] ===> Server
(task#2) : iteration#3 finished
# 53 [utest.c, 717; t_server_f2 , #360000] ---> Server
(task#2) : iteration#4 started

----------------------------------
### Running ######################
### SHARE_NAMED_SOCKET undefined #
### Part#5.2 : END ###############
----------------------------------

===========================================
=== END OF THE MESSAGE ====================
===========================================


--

============================
Alex Vinokur
mailto:ale...@bigfoot.com
mailto:al...@scopus.co.il
http://go.to/alexv_math
============================

--
Posted from dns.scopus.co.il [194.90.203.130]

0 new messages