Transferring Files Over RS232 C Program

Algorithm Steps:

Server:

1. Start.
2. Open the file recvfile.txt in write mode.
3. Receive the port number and establish socket connection.
4. Listen to client’s request using system call listen() .
5. While the message size is not zero, receive the client message and write it in to a file.
6. Close the socket connection,
7. Stop.

Client:
1. Start.
2. Pass server address and file name as command line arguments.
3. Open the file and establish connection using socket().
4. Using memset(), get the size of client address.
5. Connect to server using connect()system call.
6. While not end of file, print the contents into the file.
7. Close the socket connection.
8. Stop.

Syntax:
1. socket()
socket(protocol family, type of socket, protocol);

2. connect()
connect(client socketid, client address, length of server address);

3. send()
send(int s, void $buf, size_tlen, int flags);

4. recv()
recv(int s, void $buf, size_tlen, int flags);

5. listen()
listen(server socket, protocol type for number of connection) ;

6. accept()
accept(socket id, client information, client size);

//RS232 Server Program:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<arpa/inet.h>
#define MAX 5
#define BUF 256
int main(int argc, char **argv)
{
    int sersock, clisock, portno;
    struct sockaddr_in echoseraddr,echocliaddr;
    char echobuffer[BUF];
    unsigned int clilen, recvmsgsize;
    FILE *fp=fopen(“recvfile.txt”,”w”);
    portno=atoi(argv[1]);
    sersock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    memset(&echoseraddr,0,sizeof(&echoseraddr));
    echoseraddr.sin_family=AF_INET;
    echoseraddr.sin_addr.s_addr=htonl(INADDR_ANY);
    echoseraddr.sin_port=htons(portno);
    bind(sersock,(struct sockaddr*)&echoseraddr,sizeof(echoseraddr));
    printf(“\nServer waiting for client on port %d”, portno);
    listen(sersock,MAX);
    clilen=sizeof(echocliaddr);
    clisock=accept(sersock,(struct sockaddr *)&echocliaddr, &clilen);
    recvmsgsize=1;
    while(recvmsgsize>0)
    {
        recvmsgsize=recv(clisock,echobuffer,BUF,0);
        echobuffer[recvmsgsize]=’\0’;
        fprintfp(fp,”%s”,echobuffer);
    }
    printf(“\nFile received and socket closed”);
    close(clisock);
    fclose(fp);
    return 0;
}
//RS232 Client Program:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<unistd.h>
#include<arpa/inet.h>
#define BUF 256
int main(int argc, char **argv)
{
    int sock, portno;
    struct sockaddr_in echoseraddr;
    char *serIP, *filename, echobuffer[32];
    FILE *fp;
    serIP=argv[1];
    filename=argv[2];
    portno=atoi(argv[3]);
    fp=fopen(filename,”r”);
    sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    memset(&echoseraddr,0,sizeof(&echoseraddr));
    echoseraddr.sin_family=AF_INET;
    echoseraddr.sin_addr.s_addr=inet_addr(serIP);
    echoseraddr.sin_port=htons(portno);
    connect(sock,(struct sockaddr *)&echoseraddr, sizeof(echoseraddr));
    while(!feof(fp))
    {
        fgets(echobuffer,BUF,fp);
        send(sock,echobuffer,strlen(echobuffer),0);
    }
    printf(“\nThe %s send over RS232”,filename);
    close(sock);
    return 0;
}
OUTPUT:

In Server Side:
cc rsser.c
./a.out 8222
Server waiting for client on port 8222
File received and socket closed
cat recvfile.txt
this is rs232 program

In Client Side:
cc rscli.c
./a.out 197.168.2.100 rsinput.txt 8222
The rsinput.txt send over RS232
cat > rsinput.txt
this is rs232 program

Post a Comment

0 Comments