473,386 Members | 1,786 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

regarding implemenatin of rtp

hi everyone ,please help me..
I want to implement the code of rtp but havn't get success. I hav implemented the socket programming part but now want to attach the headers of rtp with any file of the disk and after successfull transfer of the file between clent and server ,it should print the content of the file with the headers ..
Here is the code...
/************************************************** ****************************/
main.cpp
/************************************************** **************************/

#include "wcomm.h"

void runclient(char *ip, char *fpath);
void runserver();


WComm w;

void main(int argc, char *argv[])
{

if(argc==1)runserver();
else runclient(argv[1],argv[2]);
}


void runserver()
{
// Start Server Daemon
w.startServer(12345);
printf("Server Started........\n");

while (TRUE) {

// Wait until a client connects
w.waitForClient();
printf("Client Connected......\n");

// Work with client
while(TRUE)
{
char rec[50] = "";
w.recvData(rec,32);
w.sendData("OK");

if(strcmp(rec,"FileSend")==0)
{
char fname[32] ="";
w.fileReceive(fname);
printf("File Received.........\n");
}


if(strcmp(rec,"EndConnection")==0)break;
printf("Connection Ended......\n");

}

// Disconnect client
w.closeConnection();
}

}

void runclient(char *ip, char *fpath)
{
char rec[32] = "";

// Connect To Server
w.connectServer(ip,123455);
printf("Connected to server...\n");

// Sending File
w.sendData("FileSend");
w.recvData(rec,32);
w.fileSend(fpath);
printf("File Sent.............\n");

// Send Close Connection Signal
w.sendData("EndConnection");
w.recvData(rec,32);
printf("Connection ended......\n");
}

/************************************************** **************************/
wcomm.cpp
/************************************************** **********************/

#include "wcomm.h"

WSADATA wsaData;
SOCKET m_socket;
SOCKET m_backup;
sockaddr_in con;
SOCKET AcceptSocket;

WComm::WComm()
{

// Initialize Winsock.
int iResult = WSAStartup( MAKEWORD(2,2), &wsaData );
if ( iResult != NO_ERROR )
printf("Error at WSAStartup()\n");

// Create a socket.
m_socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );

if ( m_socket == INVALID_SOCKET ) {
printf( "Error at socket(): %ld\n", WSAGetLastError() );
WSACleanup();
return;
}

m_backup = m_socket;


}



void WComm::connectServer(char *ip,int port)
{
// Connect to a server.
con.sin_family = AF_INET;
con.sin_addr.s_addr = inet_addr( ip );
con.sin_port = htons( port);

if ( connect( m_socket, (SOCKADDR*) &con, sizeof(con) ) == SOCKET_ERROR) {
printf( "Failed to connect.\n" );
WSACleanup();
return;
}
}


void WComm::startServer(int port)
{
// Connect to a server.
con.sin_family = AF_INET;
con.sin_addr.s_addr = inet_addr( "0.0.0.0" );
con.sin_port = htons( port );

if ( bind( m_socket, (SOCKADDR*) &con, sizeof(con) ) == SOCKET_ERROR) {
printf( "Failed to connect.\n" );
WSACleanup();
return;
}

// Listen on the socket.
if ( listen( m_socket, 1 ) == SOCKET_ERROR )
printf( "Error listening on socket.\n");

}

void WComm::waitForClient()
{
AcceptSocket = SOCKET_ERROR;
while ( AcceptSocket == SOCKET_ERROR ) {
AcceptSocket = accept( m_backup, NULL, NULL );
}
m_socket = AcceptSocket;
}

int WComm::sendData(char *sendbuf)
{
return send( m_socket, sendbuf, strlen(sendbuf), 0 );
}


int WComm::recvData(char *recvbuf,int size)
{
int sz = recv( m_socket, recvbuf, size, 0 );
recvbuf[sz] = '\0';
return sz;
}


void WComm::closeConnection()
{
closesocket(m_socket);
m_socket = m_backup;
}


void WComm::fileReceive(char *filename)
{

char rec[50] = "";


recv( m_socket, filename, 32, 0 );
send( m_socket, "OK", strlen("OK"), 0 );

FILE *fw = fopen(filename, "wb");

int recs = recv( m_socket, rec, 32, 0 );
send( m_socket, "OK", strlen("OK"), 0 );

rec[recs]='\0';
int size = atoi(rec);


while(size > 0)
{
char buffer[1030];

if(size>=1024)
{
recv( m_socket, buffer, 1024, 0 );
send( m_socket, "OK", strlen("OK"), 0 );
fwrite(buffer, 1024, 1, fw);

}
else
{
recv( m_socket, buffer, size, 0 );
send( m_socket, "OK", strlen("OK"), 0 );
buffer[size]='\0';
fwrite(buffer, size, 1, fw);
}


size -= 1024;

}

fclose(fw);

}

void WComm::fileSend(char *fpath)
{

// Extract only filename from given path.
char filename[50];
int i=strlen(fpath);
for(;i>0;i--)if(fpath[i-1]=='\\')break;
for(int j=0;i<=(int)strlen(fpath);i++)filename[j++]=fpath[i];


ifstream myFile (fpath, ios::in|ios::binary|ios::ate);
int size = (int)myFile.tellg();
myFile.close();

char filesize[10];itoa(size,filesize,10);


send( m_socket, filename, strlen(filename), 0 );
char rec[32] = "";recv( m_socket, rec, 32, 0 );

send( m_socket, filesize, strlen(filesize), 0 );
recv( m_socket, rec, 32, 0 );


FILE *fr = fopen(fpath, "rb");

while(size > 0)
{
char buffer[1030];

if(size>=1024)
{
fread(buffer, 1024, 1, fr);
send( m_socket, buffer, 1024, 0 );
recv( m_socket, rec, 32, 0 );

}
else
{
fread(buffer, size, 1, fr);
buffer[size]='\0';
send( m_socket, buffer, size, 0 );
recv( m_socket, rec, 32, 0 );
}


size -= 1024;

}

fclose(fr);

}
/************************************************** *******************/
wcomm.h
/************************************************** ****************/
#include "winsock2.h"
#include <stdio.h>
#include <conio.h>
#include "iostream.h"
#include "fstream.h"


class WComm
{

public:

WComm();

void connectServer(char*,int);
int sendData(char*);
int recvData(char*,int);
void fileSend(char*);
void fileReceive(char*);
void startServer(int);
void waitForClient();
void closeConnection();
};
Mar 25 '08 #1
0 1293

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Francis Lavoie | last post by:
Hello I have some questions regarding webframework, I must say that I quite lost and these questions are basicly to help me understand the way it work. I have some knowledge with PHP and JSP....
3
by: praba kar | last post by:
Dear All, I am new to Python. I am in need of some sorting functions (eg) numerical sorting functions and alphapetical sorting functions. I have searched through net But I cannot find any...
3
by: Samuel | last post by:
I wrote a very simple httpmodule and tried to compile it with no success. This is my code: ============== Imports System Imports System.Web Imports Microsoft.VisualBasic NameSpace...
7
by: Squignibbler | last post by:
Hi all, I have a question regarding the C++ programming language regarding the nature of the relationship between pointers and arrays. If the statement MyArray is functionally identical to...
8
by: Mike | last post by:
Hello, I have a few rather urgent questions that I hope someone can help with (I need to figure this out prior to a meeting tomorrow.) First, a bit of background: The company I work for is...
2
by: Dean R. Henderson | last post by:
For an ASP.NET web application, is there a way for one session (with appropriate security authorization) to set a HttpSessionState variable to point to another session and execute the Abandon...
5
by: archana | last post by:
Hi all, I am using timer to do some functionality on user specified time. I am using system.timers.timer class and its timer to do this functionality. What i am doing is i set autoreset to...
1
by: Dave | last post by:
Hi guys, i have a small question regarding register_globals I have managed to change the way my forms work so they will process with register_globals turned off. However i have one page which...
1
by: Jim Flanagan | last post by:
Hello - I am in need of more help regarding an approach to accomplishing the following: We have a need to change the Volume serial numbers of a bunch of preprogrammed IDE Solid State Drive...
8
by: somenath | last post by:
Hi All, I have a doubt regarding the pointer assignment . Please have a look at the following program . #include<stdio.h> #include<stdlib.h> #define NAMESIZE 10 #define SAFE_FREE(t) if(t)\...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.