EmbLogic's Blog

Serial Port Communication

Serial Port Communication

 

Serial communication is the process of sending data one bit at a time, sequentially, over a communication channel or computer bus where as in case of parallel communication, where several bits are sent as a whole, on a link with several parallel channels. Serial communication is used for all long distance communication and most computer networks, where the difficulties like cost and synchronization make parallel communication difficult. Serial communication mostly refers to the RS-232 communication protocol where 9 pin connectors used for communication between 2 devices.

RS-232 Protocol:

RS-232 is the name for a standards for serial binary single-ended data and control signals connecting between data terminal equipment and data circuit-terminating equipment. It is commonly used in computer serial ports. The standard defines the electrical characteristics and signals timing, size and pin-out of connectors. This protocol defines the maximum open-circuit voltage of +/- 25 volts. Valid signals are in the range of +3 to +15 volts or the range ?3 to ?15 volts with respect to the ground. The range between ?3 to +3 volts is not a valid RS-232 level. For data transmission lines (TxD, RxD etc.) logic one is defined as a negative voltage and the condition is called mark. Logic zero is +ve and the signal condition is termed space. Control signals have the opposite polarity: the active state is positive voltage and the inactive state is negative voltage.

The following is commonly used RS-232 signals and pin:

  • Pin 1 = CT (Carrier Detect)

  • Pin 2 = RxD (Received Data)

  • Pin 3 = TxD (Transmitted Data)

  • Pin 4 = DTR (Data Terminal Ready)

  • Pin 5 = Ground

  • Pin 6 = DSR (Data Set Ready)

  • Pin 7 = RTS (Request To Send)

  • Pin 8 = CTS (Clear To Send)

  • Pin 9 = RI (Ring Indicator)

Serial Port base Address and IRQ Number:

Serial Port has following base addresses and IRQ number associated with it:

      1. 3F8 4

      2. 2F8 3

      3. 3E8 4

      4. 2E8 3

The most commonly used UART IC in personal computers is pc16550D. It has 8 registers which are used for controlling the data transmission. They are:

Register 0: Receiver Buffer Register(Read Only) & Transmitter Register(Write Only).

These buffer are used for receiving and transmitting data between two devices.

Register 1: Interrupt Enable register

This register is used to enabling the interrupts. Setting bit 0-3 will enable Receive Data Available, Transmitter empty, Line Status Interrupt and Modem Status Interrupt.

Register 2 : Interrupts Identification Register(R/O) & FIFO Control Register (W/O)

IIR : If any interrupt has occurred, reading out this register will give the corresponding interrupt value.

FCR : This will enable the FIFO mode for UART i.e. it will store up-to 14 bytes of data before transmission.

Register 3 : Line Control Register :

This register controls the transmission pattern of data. It is also used to set the baud rate of the UART by setting bit 7 and writing value in Divisor latch buffers.

Register 4 : Modem Control Register:

Used to start the handshaking mechanism between host and peripheral.

Register 5: Line status Register :

This register is used to get the line status and if data is available. It is a read only register.

Register 6: Modem Status Register:

This register shows the current state of data line when read.

Register 7: Scratch Register

Divisor Latch (LS) & (MS) :

These register together forms a 16bit register when the bit7 of LCR register is set. They hold the multiplier value, to set the baud rate of the UART at desired rate.

Data Transmission:

First the transmitter will indicate the receiver by setting is MCR bit0 which will notify the receiver at MSR bit5 as pin 4 of transmitter is connected to pin 6 of receiver. After receiving the signal DSR, receiver will set its MCR bit1 which is connected to transmitter’s RTS pin and will notify the transmitter that he is ready to receive data. Then transmitter will put the data in Transmitter buffer and receiver will receive the data in receiver buffer.

Posted in Device Drivers, Serial Port Driver | Leave a comment

NFS SERVER CONFIGURATION

NFS, or Network File System, is a server-client protocol for sharing files between computers on a common network. NFS enables you to mount a file system on a remote computer as if it were local to your own system. You can then directly access any of the files on that remote file system. The server and client do not have to use the same operating system. The client system just needs to be running an NFS client compatible with the NFS server.

 

For example NFS server could be a Linux system and Unix could be a client. But it can’t be a window system because window is not NFS compatible. The NFS server exports one or more directories to the client systems, and the client systems mount one or more of the shared directories to local directories called mount points. After the share is mounted, all I/O operations are written back to the server, and all clients notice the change as if it occurred on the local filesystem.

 

A manual refresh is not needed because the client accesses the remote filesystem as if it were local.because access is granted by IP address, a username and password are not required. However, there are security risks to consider because the NFS server knows nothing about the users on the client system.

 

Configure nfs server

 

  • Network configuration in Linux

 

  1. A linux server with ip address 192.168.0.254 and hostname Server
  2. A linux client with ip address 192.168.0.1 and hostname Client1
  3. Updated /etc/hosts file on both linux system
  4. Running portmap and xinetd services
  5. Firewall should be off on server

 

  • Three rpm are required to configure nfs server. nfs, portmap, xinetd check them if not found then install
  1. Now check nfs, portmap, xinetd service in system service it should be on
#setup
Select  System service
from list
[*]portmap [*]xinetd [*]nfs

2.Now restart xinetd and portmap service

  1. To keep on these services after reboot on then via chkconfig command
  2. After reboot verify their status. It must be in running condition
  3. now create a /data directory and grant full permission to it
  4. now open /etc/exports filesave file with :wq and exit
  5. also restart nfs daemons with expotfs

 

  1. verify with showmount command that you have successfully shared data folder

configure client system

  1. ping form nfs server and check the share folder
  2. now mount this share folder on mnt mount point. To test this share folder change directory to mnt and create a test file
  3. After use you should always unmount from mnt mount point
  4. create a mount point, by making a directory
  5. now open /etc/fstab file
  6. after reboot check /temp directory it should show all the shared data
Posted in Project 00: Linux System / Network Administration | Leave a comment

Client Server Endpoints Communication using AF_UNIX Sockets [RCS Log]

SERVER RCS

head 1.3;
access;
symbols;
locks; strict;
comment @ * @;
1.3
date 2013.12.30.07.41.22; author root; state Exp;
branches;
next 1.2;
1.2
date 2013.12.29.10.18.09; author root; state Exp;
branches;
next 1.1;
1.1
date 2013.12.29.09.45.45; author root; state Exp;
branches;
next ;
desc
@Server Program is ready to roll ..
@
1.3
log
@after few modifications server will now read data from socket, and increment the the recieved character by one and will write again: New Functionality Added in this version.
@

 
CLIENT RCS

head 1.3;
access;
symbols;
locks; strict;
comment @ * @;

1.3
date 2013.12.30.07.44.46; author root; state Exp;
branches;
next 1.2;

1.2
date 2013.12.29.10.18.44; author root; state Exp;
branches;
next 1.1;

1.1
date 2013.12.29.10.06.57; author root; state Exp;
branches;
next ;

desc
@Client program
@

1.3
log
@in this version client will connect to server and will write a character on socket, and after procesing from server, it will read and display the character from socket.
@

Posted in Uncategorized | Leave a comment

Introduction to GRUB

Briefly, a boot loader is the first software program that runs when a computer starts. It
is responsible for loading and transferring control to an operating system kernel software
(such as Linux or GNU Mach). The kernel, in turn, initializes the rest of the operating
system (e.g. a GNU system).
GNU GRUB is a very powerful boot loader, which can load a wide variety of free
operating systems, as well as proprietary operating systems with chain-loading. GRUB
is designed to address the complexity of booting a personal computer; both the program
and this manual are tightly bound to that computer platform, although porting to other
platforms may be addressed in the future.
One of the important features in GRUB is flexibility; GRUB understands filesystems
and kernel executable formats, so you can load an arbitrary operating system the way you like, without recording the physical position of your kernel on the disk. Thus you can load the kernel just by specifying its file name and the drive and partition where the kernel resides.

GRUB features

The primary requirement for GRUB is that it be compliant with the Multiboot Specification, which is described in Section “Motivation” in The Multiboot Specification.
The other goals, listed in approximate order of importance, are:
 Basic functions must be straightforward for end-users.
 Rich functionality to support kernel experts and designers.
 Backward compatibility for booting FreeBSD, NetBSD, OpenBSD, and Linux. Proprietary kernels (such as DOS, Windows NT, and OS/2) are supported via a chain-loading function.

Posted in Uncategorized | Leave a comment

An Introduction to Block Driver

An Introduction to Block Driver

When Unix was written 25 years ago, its design was eclectic. One unusual design feature was that every physical device connected to the computer was represented as a file. This was a bold decision, because many devices are very different from one another, especially at first glance. Why use the same interface to talk to a printer as to talk to a disk drive?

The short answer is that while the devices are very much different, they can be thought of as having most of the same characteristics as files. The entire system is then kept smaller and simpler by only using one interface with a few extensions.

This is fine, except that it hides important differences between devices. For example, it is possible to read any byte on a disk at any time, but it is only possible to read the next byte from a terminal.

There are other differences, but this is the most fundamental one: Some devices (like disks) are random-access, and others (like terminals) are sequential-access. Of course, it is possible to pretend that a random-access device is a sequential-access device, but it doesn’t work the other way around.

A practical effect of the difference is that file systems can only be mounted on block devices, not on character ones. For example, most tapes are character devices. It is possible to copy the contents of a raw, quiescent (unmounted and not being modified) file system to a tape, but you will not be able to mount the tape, even though it contains the same information as the disk.

Most textbooks and tutorials start by explaining character devices, the sequential-access ones, because a minimal character device driver is easier to write than a minimal block device driver. My own Linux Kernel Hackers’ Guide (the KHG) is written the same way.

My reason for starting this column with block devices, the random-access devices, is that the KHG explains simple character devices better than it does block devices, and I think that there is a greater need for information on block devices right now. Furthermore, real character device drivers can be quite complex, just as complex as block device drivers, and fewer people know how to write block device drivers.

I am not going to give a complete example of a device driver here. I am going to explain the important parts, and let you discover the rest by examining the Linux source code. Reading this article and the ramdisk driver (drivers/block/ramdisk.c), and possibly some parts of the KHG, should make it possible for you to write a simple, non-interrupt-driven block device driver, good enough to mount a filesystem on. To write an interrupt-driven driver, read drivers/block/hd.c, the AT hard disk driver, and follow along. I’ve included a few hints in this article, as well.

The Heart of the Driver

Whereas character device drivers provide procedures for directly reading and writing data from and to the device they drive, block devices do not. Instead, they provide a single request() procedure which is used for both reading and writing. There are generic block_read() and block_write() procedures which know how to call the request() procedure, but all you need to know about those functions is to place a reference to them in the right place, and that will be covered later.

The request() procedure (perhaps surprisingly for a function designed to do I/O) takes no arguments and returns void. Instead of explicit input and return values, it looks at a queue of requests for I/O, and processes the requests one at a time, in order. (The requests have already been sorted by the time the request() function reads the queue.) When it is called, if it is not interrupt-driven, it processes requests for blocks to be read from the device, until it has exhausted all pending requests. (Normally, there will be only one request in the queue, but the request() procedure should check until it is empty. Note that other requests may be added to the queue by other processes while the current request is being processed.)

On the other hand, if the device is interrupt-driven, the request() procedure will usually schedule an interrupt to take place, and then let the interrupt handling procedure call end_request() (more on end_request() later) and then call the request() procedure again to schedule the next request (if any) to be processed.

 

Posted in Block Driver | Leave a comment

SOCKETS

Sockets are an inter-process network communication implementation using a Internet Protocol (IP) stack on an Ethernet transport. Sockets are language and protocol independent and available to “C”, Perl, Python, Ruby and Java (and more) programmers. The “C” language BSD API is used on Linux, all popular variants of Unix, Microsoft Windows (NT,2000,XP,… and later) and even embedded OSs like VxWorks. It is by far the most popular implementation of inter-process network communication.

Sockets allow one process to communicate with another whether it is local on the same computer system or remote over the network. Many other higher level protocols are built upon sockets technology.

The sockets API provides many configuration options so we will try and cover the socket API components and then give examples of a few implementations. It would be very difficult to cover all variations of its use.

Posted in Uncategorized | Leave a comment

AN OVERVIEW OF PROCESS COMMUNICATION IN LINUX

An Overview of Process Communication in Linux

First of all, let’s understand the actual meaning of: IPC. IPC is an abbreviation that stands for Inter-process Communication. It denotes a set of system calls that allows a User Mode process to:

  1. Synchronize itself with other processes by means of ‘Semaphores’.
  2. Send messages to other processes or receive messages from them.
  3. Share a memory area with other processes.

IPC was introduced in a development UNIX variant called “Columbus Unix” and later adopted by AT&T’s System III. It is now commonly found in most UNIX systems,  and provides three methods of communication: message queues, semaphores, and shared segments. Like BSD mmap, System V IPC uses files to identify shared segments. Unlike BSD, System V uses these files only for naming. Their contents have nothing to do with the initialization of the shared segment. IPC data structures are created dynamically when a process requests an IPC Resource, i.e. a semaphore, a message queue, or a shared memory segment. All of these IPC Resources would be discussed in detail later on. Before we dive deep into the subject matter, there are a few things that I would like to explain at the very beginning. They are as follows:

  1. The mechanism in which User Mode processes synchronize themselves and exchange data is referred to as “Inter-process Communication (IPC)” in UNIX Systems (that includes Linux too). But in what way exactly do terms like: Semaphores, Shared Memory and Message Queues relate to IPC? All readers must note that Semaphores, Shared Memory and Message Queues do relate to IPC in a very special way, since Semaphores, Shared Memory and Message Queues are “Inter-process Communication Resources” or “Inter-process Communication Facilities”, and different in the way they represent IPC from “Inter-process Communication Mechanisms” like Pipes and FIFOs. Semaphores, Shared Memory and Message Queues are System V (AT&T System V.2 release of UNIX) IPC facilities, and they represent wrapper functions that have been developed and inserted in suitable libraries to harness the energy and beauty of IPC mechanisms. More on this later.
  2. Data sharing among processes can be obtained by storing data in temporary files protected by locks. But this mechanism is never implemented as it proves costly since it requires accesses to the disk filesystem. For that reason, all UNIX Kernels include a set of system calls that supports process communications without interacting with the filesystem.

Application programmers have a variety of needs that call for different communication mechanisms. Some of the basic mechanisms that UNIX systems, GNU/Linux is particular has to offer are:

  1. Pipes and FIFOs: Mainly used for implementing producer/consumer interactions among processes. Some processes will fill the pipe with data while others will extract from it.
  2. Semaphores: Here we refer to (NOT the POSIX Realtime Extension Semaphores applied to Linux Kernel Threads), but System V semaphores which apply to User Mode processes. Used for locking critical sections of code.
  3. Message Queues: To set up a message queue between processes is a way to exchange short blocks (called messages) between two processes in an asynchronous way.
  4. Shared Memory: A mechanism (specifically a resource) applied when processes need to share large amounts of data in an efficient way.

Another commonly used data communication mechanism in networks, “Sockets” will NOT be discussed here since it requires a long discussion of networking. In this article, we will explore all the above-mentioned IPC mechanisms and System V IPC facilities at our disposal.

Posted in Uncategorized | Leave a comment

A Brief Introduction about Socket-

A socket is a communication mechanism that allows client/server systems to be developed either locally, on a single machine, or across networks. Linux functions such as printing, connecting to databases, and
serving web pages as well as network utilities such as rlogin for remote login and ftp for file transfer usually use sockets to communicate.
Sockets are created and used differently from pipes because they make a clear distinction between client and server. The socket mechanism can implement multiple clients attached to a single server.

In simple words we can say, Sockets are basically an extension of pipe.
First, a server application creates a socket, which like a file descriptor is a resource assigned to the server
process and that process alone. The server creates it using the system call socket, and it can’t be shared
with other processes.
The server process gives the socket a name. Local sockets are given a filename in the Linux file system, often to be found in /tmp or /usr/tmp. For network sockets, the filename will be a service identifier (port number/access point) relevant to the particular network to which the clients can connect. This identifier allows Linux to route incoming connections specifying a particular port number to the correct server process.A socket is named using the system call bind. The server process then waits for a client to connect to the named socket. The system call, listen, creates a queue for incoming connections. The server can accept them using the system call accept. When the server calls accept, a new socket is created that is distinct from the named socket. This new socket is used solely for communication with this particular client. The named socket remains for further connections from other clients. If the server is written appropriately, it can take advantage of multiple connections. A web server will do this so that it can serve pages to many clients at once. For a simple server, further clients wait on the listen queue until the server is ready again. The client side of a socket-based system is more straight forward. The client creates an unnamed socket by calling socket. It then calls connect to establish a connection with the server by using the server’s named socket as an address. Once established, sockets can be used like low-level file descriptors, providing two-way data communications.

Posted in Uncategorized | Leave a comment

Advantage and Drawbacks of Threads

Following are some advantages of using threads:

  1. Sometimes it is very useful to make a program appear to do two things at once. The classic example is to perform a real-time word count on a document while still editing the text. One thread can manage the user’s input and perform editing. The other, which can see the same document content, can continuously update a word count variable. The first thread (or even a third one) can use this shared variable to keep the user informed. Another example is a multithreaded database server
    where an apparent single process serves multiple clients, improving the overall data throughput by servicing some requests while blocking others, waiting for disk activity. For a database server, this apparent multitasking is quite hard to do efficiently in different processes, because the  requirements for locking and data consistency cause the different processes to be very tightly coupled. This can be done much more easily with multiple threads than with multiple processes.
  2. The performance of an application that mixes input, calculation, and output may be improved by running these as three separate threads. While the input or output thread is waiting for a connection, one of the other threads can continue with calculations. A server application pro-cessing multiple network connects may also be a natural fit for a multithreaded program.
  3. Now that multi-cored CPUs are common even in desktop and laptop machines, using multiple threads inside a process can, if the application is suitable, enable a single process to better utilize the hardware resources available.
  4. In general, switching between threads requires the operating system to do much less work than switching between processes. Thus, multiple threads are much less demanding on resources than multiple processes, and it is more practical to run programs that logically require many threads of execution on single-processor systems. That said, the design difficulties of writing a multithreaded program are significant and should not be taken lightly.

Threads also have drawbacks:

  1. Writing multithreaded programs requires very careful design. The potential for introducing subtle timing faults, or faults caused by the unintentional sharing of variables in a multithreaded program is considerable. Alan Cox (the well respected Linux guru) has commented that threads are also known as “how to shoot yourself in both feet at once.”
  2. Debugging a multithreaded program is much, much harder than debugging a single-threaded one, because the interactions between the threads are very hard to control.
  3. A program that splits a large calculation into two and runs the two parts as different threads will not necessarily run more quickly on a single processor machine, unless the calculation truly allows multiple parts to be calculated simultaneously and the machine it is executing on has multiple processor cores to support true multiprocessing.
Posted in Uncategorized | Leave a comment

Advantages and Disadvantages of Semaphores

 

Advantages

In semaphores there is no spinning, hence no waste of resources due to no busy waiting. That is because threads intending to access the critical section are queued. And could access the priority section when the are de-queued, which is done by the semaphore implementation itself, hence, unnecessary CPU time is not spent on checking if a condition is satisfied to allow the thread to access the critical section.

Semaphores permit more than one thread to access the critical section, in contrast to alternative solution of synchronization like monitors, which follow the mutual exclusion principle strictly. Hence, semaphores allow flexible resource management.
Finally, semaphores are machine independent, as they are implemented in the machine independent code of the microkernel services.

Disadvantages

Problem 1: Programming using Semaphores makes life harder as utmost care must be taken to ensure Ps and Vs are inserted correspondingly and in the correct order so that mutual exclusion and deadlocks are prevented. In addition, it is difficult to produce a structured layout for a program as the Ps and Vs are scattered all over the place. So the modularity is lost. Semaphores are quite impractical when it comes to large scale use.

Problem 2: Semaphores involve a queue in its implementation. For a FIFO queue, there is a high probability for a priority inversion to take place wherein a high priority process which came a bit later might just have to wait when a low priority one is in the critical section. For example, consider a case when a new smoker joins and is desperate to smoke. What if the agent who handles the distribution of the ingredients follows a FIFO queue (wherein the desperate smoker is last according to FIFO) and chooses the ingredients apt for another smoker who would rather wait some more time for a next puff?

Posted in Uncategorized | Leave a comment

SOCKET IMPLIMENTATION

RCS LOG FILE

SERVER.C
head
1.1;
access;
symbols;
locks
deepak:1.1; strict;
comment @ * @;

1.1
date 2013.12.30.01.10.12; author deepak; state Exp;
branches;
next ;

desc
@server is created to test socket.
@

1.1
log
@Initial revision
________________________________________________________________________
CLIENT.C
head 1.1;
access;
symbols;
locks
deepak:1.1; strict;
comment @ * @;

1.1
date 2013.12.30.00.55.28; author deepak; state Exp;
branches;
next ;

desc
@client for socket is creted.
@

1.1
log
@Initial revision
________________________________________________________________________
HED.H

head 1.1;
access;
symbols;
locks
deepak:1.1; strict;
comment @ * @;

1.1
date 2013.12.30.00.56.46; author deepak; state Exp;
branches;
next ;

desc
@header file common for server and client is created.
@

1.1
log

Posted in Uncategorized | Leave a comment

BRIEF INTRODUCTION OF TCP/IP USED IN SOCKETS

The Internet protocol suite is the networking model and a set of communications protocols used for the Internet and similar networks. It is commonly known as TCP/IP, because its most important protocols, the Transmission Control Protocol (TCP) and the Internet Protocol (IP), were the first networking protocols defined in this standard. It is occasionally known as the DoD model, because the development of the networking model was funded by DARPA, an agency of the United States Department of Defense.

TCP/IP provides end-to-end connectivity specifying how data should be formatted, addressed, transmitted, routed and received at the destination. This functionality has been organized into four abstraction layers which are used to sort all related protocols according to the scope of networking involved.[1][2] From lowest to highest, the layers are the link layer, containing communication technologies for a single network segment (link), the internet layer, connecting independent networks, thus establishing internetworking, the transport layer handling process-to-process communication, and the application layer, which interfaces to the user and provides support services.

KEY ARCHITECTURE

1.> End-to-end principle: This principle has evolved over time. Its original expression put the maintenance of state and overall intelligence at the edges, and assumed the Internet that connected the edges retained no state and concentrated on speed and simplicity. Real-world needs for firewalls, network address translators, web content caches and the like have forced changes in this principle.[13]
2.> Robustness Principle: “In general, an implementation must be conservative in its sending behavior, and liberal in its receiving behavior. That is, it must be careful to send well-formed datagrams, but must accept any datagram that it can interpret (e.g., not object to technical errors where the meaning is still clear).” [14] “The second part of the principle is almost as important: software on other hosts may contain deficiencies that make it unwise to exploit legal but obscure protocol features.

Posted in Uncategorized | Leave a comment

Article on socket

Socket

Introduction

The endpoint in an interprocess communication is called a socket, or a network socket for disambiguation. Since most communication between computers is based on the Internet Protocol, an almost equivalent term is Internet socket. The data transmission between two sockets is organised by communication protocols.usually implemented in the operating system of the participating computers. Application programs write to and read from these sockets. Therefore, network programming is essentially socket programming.

Programming with TCP/IP sockets

There are a few steps involved in using sockets:

  1. Create the socket
  2. Identify the socket
  3. On the server, wait for an incoming connection
  4. On the client, connect to the server’s socket
  5. Send and receive messages
  6. Close the socket

Step 1. Create a socket

A socket, s, is created with the socket system call:

int s = socket(domain, type, protocol)

All the parameters as well as the return value are integers:

domain, or address family —
communication domain in which the socket should be created. Some of address families are AF_INET (IP), AF_INET6 (IPv6), AF_UNIX (local channel, similar to pipes), AF_ISO (ISO protocols), and AF_NS (Xerox Network Systems protocols).
type —
type of service. This is selected according to the properties required by the application: SOCK_STREAM (virtual circuit service), SOCK_DGRAM (datagram service), SOCK_RAW (direct IP service). Check with your address family to see whether a particular service is available.
protocol —
indicate a specific protocol to use in supporting the sockets operation. This is useful in cases where some families may have more than one protocol to support a given type of service. The return value is a file descriptor (a small integer). The analogy of creating a socket is that of requesting a telephone line from the phone company.

For TCP/IP sockets, we want to specify the IP address family (AF_INET) and virtual circuit service (SOCK_STREAM). Since there’s only one form of virtual circuit service, there are no variations of the protocol, so the last argument, protocol, is zero.

Step 2. Indentify (name) a socket

When we talk about naming a socket, we are talking about assigning a transport address to the socket (a port number in IP networking). In sockets, this operation is called binding an address and the bind system call is used for this. The analogy is that of assigning a phone number to the line that you requested from the phone company in step 1 or that of assigning an address to a mailbox.

The transport address is defined in a socket address structure. Because sockets were designed to work with various different types of communication interfaces, the interface is very general. Instead of accepting, say, a port number as a parameter, it takes a sockaddr structure whose actual format is determined on the address family (type of network) you’re using. For example, if you’re using UNIX domain sockets, bind actually creates a file in the file system.

The system call for bind is:

#include <sys/socket.h>

int bind(int socket, const struct sockaddr *address, socklen_t address_len);

Step 3a. Connect to a server from a client

If we’re a client process, we need to establish a connection to the server. Now that we have a socket that knows where it’s coming from, we need to tell it where it’s going to. The connect system call accomplishes this.

#include <sys/types.h>
#include <sys/socket.h>
int connect(int socket, const struct sockaddr *address, socklen_t address_len);

Step 3b. Accept connections on the server

Before a client can connect to a server, the server should have a socket that is prepared to accept the connections. The listen system call tells a socket that it should be capable of accepting incoming connections:

#include <sys/socket.h> int listen(int socket, int backlog);

Step 4. Communicate

We finally have connected sockets between a client and a server! Communication is the easy part. The same read and write system calls that work on files also work on sockets. We can send 20 bytes from the client to server with:

char buffer[MAXBUF]; … nbytes = write(fd, buffer, 20); /* write 20 bytes in buffer */

Step 5. Close the connection

When we’re done communicating, the easiest thing to do is to close a socket with the close system call — the same close that is used for files.

#include <sys/socket.h>

int close(int fd);
Posted in Uncategorized | Leave a comment

socket(client-server)

head 2.1;
access;
symbols;
locks; strict;
comment @ * @;

1.1
date 2013.12.30.11.56.58; author root; state Exp;
branches;
next ;

desc
@program is ready
@
1.1
log
@Initial revision
@
text
head ;
access;
symbols;
locks; strict;
comment @ * @;
desc
@request client
@

Posted in Uncategorized | Leave a comment

SOCKET programming (client server implementation)

SERVER LOG FILE

head    1.1;
2 access;
3 symbols;
4 locks
5         root:1.1; strict;
6 comment @ * @;
7
8
9 1.1
10 date    2013.12.29.10.12.34;    author root;    state Exp;
11 branches;
12 next    ;
13
14
15 desc
16 @@main server source code
17
18
19 1.1
20 log
21 @Initial revision
22 @
________________________________________________________________________________________________________

CLIENT LOG FILE

1 head    1.1;
2 access;
3 symbols;
4 locks; strict;
5 comment @ * @;
6
7
8 1.1
9 date    2013.12.29.10.26.00;    author root;    state Exp;
10 branches;
11 next    ;
12
13
14 desc
15 @client program is working
16 @
17
18
19 1.1
20 log
21 @Initial revision
22 @

Posted in Project 04: FTP based Client Server using Threads and Sockets | Leave a comment