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();
};