Copyright © tutorialspoint.com

Socket Helper Functions

previous next


Advertisements

This tutorial describes all the helper functions which are used while doing socket programming. Other helper functions are described in Ports and Services, Netowrk Byte Orders tutorials.

The write Function:

The write function attempts to write nbyte bytes from the buffer pointed to by buf to the file associated with the open file descriptor, fildes.

You can also use send() function to send data to another process.

#include <unistd.h>

int write(int fildes, const void *buf, int nbyte);

Upon successful completion, write() returns the number of bytes actually written to the file associated with fildes. This number is never greater than nbyte. Otherwise, -1 is returned,

Parameters:

The read Function:

The read function attempts to read nbyte bytes from the file associated with the open file descriptor, fildes, into the buffer pointed to by buf.

You can also use recv() function to read data to another process.

#include <unistd.h>

int read(int fildes, const void *buf, int nbyte);

Upon successful completion, write() returns the number of bytes actually written to the file associated with fildes. This number is never greater than nbyte. Otherwise, -1 is returned,

Parameters:

The fork Function:

The fork function create a new process. The new process is called child process will be an exact copy of the calling process (parent process). The child process inherits many attributes from the parent process.

#include <sys/types.h>
#include <unistd.h>

int fork(void);

Upon successful completion, fork() return 0 to the child process and return the process ID of the child process to the parent process. Otherwise -1 is returned to the parent process, no child process is created and errno is set to indicate the error.

Parameters:

The bzero Function:

The bzero function places nbyte null bytes in the string s. This function will be used to set all the socket structures with null values.

 void bzero(void *s, int nbyte);

This function does not return anything.

Parameters:

The bcmp Function:

The bcmp function compares byte string s1 against byte string s2. Both strings are assumed to be nbyte bytes long.

int bcmp(const void *s1, const void *s2, int nbyte);

This function returns 0 if both strings are identical, 1 otherwise. The bcmp() function always returns 0 when nbyte is 0.

Parameters:

The bcopy Function:

The bcopy function copies nbyte bytes from string s1 to the string s2. Overlapping strings are handled correctly.

void bcopy(const void *s1, void *s2, int nbyte);

This function does not return anything.

Parameters:

The memset Function:

The memset function is also used to set structure variables in the same way as bzero.

void *memset(void *s, int c, int nbyte);

This function returns a pointer to void, in fact pointer to the set memory and you need to caste it accordingly.

Parameters:


previous next

Copyright © tutorialspoint.com