Develop An Application For transferring Files Over RS232 in Server Side Programming

Step by Step Algorithm RS232: Server
Include the header files
Pass the port number as argument to main function
Open the “recvfile.txt” in write mode
Convert argument example [Port number to Integer]
Create a socket for transferring file using socket()
Fill the memory with some constant byte using memset()
Define Port family
Bind the server to the socket created
Listen to signals from client
Accept client message
Then File is transferred over RS232
Finally close the socket


Syntax for Command used
Socket
Create an end point for communication
Synopsis | command syntax
#include<sys/types.h>
#include<sys/socket.h>
int socket(int domain,int type,int protocol);

Description
Socket created an end point for communication and returns a descriptor the domain parameter specified a protocol family which will be used for communication.
Memset pointer function syntax in c language
Files Memory with constant byte

Synopsis | syntax pointer in function method
#include<string.h>
Void*memset(void *S,int e,size_t n);
Description
The memset function fills the first n bytes of the memory area pointed by s with the constant byte c . The memset function returns a pointer to the memory area S.

Atoi c function syntax with command
Synopsis
#include<stdlib.h>
Int atoi(const char *nptr);
Description
The atoi function converts the initial portion of the string pointed by nptr to int


File Transfer over RS232 in C language programming Source Code SERVER:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<arpa/inet.h>
#define BUF 256
#define MAX 5
int main(int argc,char **argv)
{
int servsock,cIntsock;
struct sockaddr_in echoServAddr,echoIntaddr;
char echoBuffer[BUF];
unsigned int cIntlen,recvMsgsize;
int portno;
FILE *fp=fopen("refile.txt","w");
portno=atoi(argv[1]);
servsock=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
memset(&echoServAddr,0,sizeof(&echoServAddr));
echoServAddr.sin_family=AF_INET;
echoServAddr.sin_addr.s_addr=htonl(INADDR_ANY);
echoServAddr.sin_port=htons(portno);
bind(servsock,(struct sockaddr*) &echoServAddr,sizeof(echoServAddr));
printf("server waiting for the client %d",portno);
listen(servsock,MAX);
cIntlen=sizeof(echoIntaddr);
cIntsock=accept(servsock,(struct sockaddr*)&echoIntaddr,&cIntlen);
recvMsgsize=1;
while(recvMsgsize>0)
{
recvMsgsize=recv(cIntsock,echoBuffer,BUF,0);
echoBuffer[recvMsgsize]='\0';
fprintf(fp,"%s",echoBuffer);
}
printf("file received from the socket closed \n");
close(cIntsock);
fclose(fp);
return(0); }


Output CS1305- NETWORK LAB
RS232 Server
[it28@localhost studentwebsite]$cc rsserver.c
[it28@localhost studentwebsite]$./a.out 80000
Server waiting for client o nport:8000
File received a socket closed
[it28@localhost studentwebsite]$cat destfile.text
Hai my studentwebsite visitor

Result
Thus the file over transferred over RS232 in server side successfully.

Related post