Click here to Skip to main content
16,011,805 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Templates and Unresolved Externals Pin
Xavier Shay13-Nov-01 22:43
Xavier Shay13-Nov-01 22:43 
GeneralRe: Templates and Unresolved Externals Pin
Krouer14-Nov-01 23:23
Krouer14-Nov-01 23:23 
GeneralRe: Templates and Unresolved Externals Pin
Oliver Anhuth15-Nov-01 22:58
Oliver Anhuth15-Nov-01 22:58 
GeneralWin32 Socket Pin
phat13-Nov-01 18:14
phat13-Nov-01 18:14 
GeneralRe: Win32 Socket Pin
Nish Nishant13-Nov-01 18:34
sitebuilderNish Nishant13-Nov-01 18:34 
GeneralRe: Win32 Socket Pin
markkuk13-Nov-01 19:38
markkuk13-Nov-01 19:38 
GeneralSockets problem Pin
Jon Sagara13-Nov-01 17:53
Jon Sagara13-Nov-01 17:53 
Generalrcopy.cpp Pin
Jon Sagara13-Nov-01 17:56
Jon Sagara13-Nov-01 17:56 
// ============================================================================
// rcopy.cc
//
// This program is repsonsible for taking the file names as command line
// arguments and communicating with the server program to request the remote-
// file. This program will then receive the file and store it to disk using
// the local-file name. Thr program will be run as:
//
// rcopy remote-file local-file remote-machine remote-port error-percent
//
// where:
//
// remote-file: is the file on the remote machine to be copied
// local-file: is the file cpied into on the local machine
// remote-machine: is the remote machine running the server
// remote-port: is the port number of the server application
// error-percent: is the percent of packets that are in error (floating
// poing number)
//
// Author: Jon Sagara
//
// This file represents the CLIENT.
// ============================================================================

#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <stream.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <strings.h>
#include <sys/time.h>

#include "networks.h"
#include "sendtoErrB.h"



int CreateUDPSocket(struct sockaddr_in &remote, char *szHostName, char *szPort);


int main(int argc, char *argv[])
{
int nSocket; // Socket descriptor.
int nBytesRead = 0; // Number of bytes read from server.
int nBytesSent = 0; // Number of bytes send to server.
int nSendLen = 0; // Number of bytes to send to server.
int nRecvLen = 0; // Number of bytes received from server.

float fErrorPct = 0.0; // Percent of packets that are in error.

sockaddr_in recv; // Socket address passed by recvfrom().
sockaddr_in remote; // Socket address for remote socket (server).

unsigned char recv_buf[BUFF_SIZE];
unsigned char send_buf[BUFF_SIZE];

//
// Check command line parameters.
//
if (6 != argc)
{
cout << "usage: " << argv[0] << " remote-file local-file"
<< "remote-machine remote-port error-percent\n";
exit(1);
}

fErrorPct = atof(argv[5]);

//
// Create the socket.
//
nSocket = CreateUDPSocket(remote, argv[3], argv[4]);
cout << "nSocket right after socket creation: " << nSocket << endl;

cout << "Enter the data to transmit: ";
cin.getline((char *) send_buf, BUFF_SIZE);

nSendLen = strlen((char *) send_buf) + 1;
cout << send_buf << " len: " << nSendLen << endl;

//
// Send data to server.
//
cout << "nSocket right before sending to server: " << nSocket << endl;
nBytesSent = sendto(nSocket, send_buf, nSendLen, 0, (struct sockaddr *) &remote, sizeof(remote));
if (nBytesSent < 0)
{
perror("Error sending data in rcopy. Trying to send to server.");
close(nSocket);
exit(1);
}

//
// Read data back from server.
//
nBytesRead = recvfrom(nSocket, recv_buf, BUFF_SIZE, 0, (struct sockaddr *) &recv, &nRecvLen);
if (nBytesRead < 0)
{
perror("Error reading data in rcopy. Trying to read back from server.");
close(nSocket);
exit(1);
}

cout << "recv_buf: " << recv_buf << endl;
cout << "nRecvLen: " << nRecvLen << endl;

close (nSocket);

return 0;
}


// ----------------------------------------------------------------------------
// CreateUDPSocket(...)
//
// Creates a UDP socket that connects to the server application on a remote
// machine. The applications port is passed in on the command line.
//
// Parameters:
// struct sockaddr_in &remote [IN/OUT]:
// Socket address for remote socket.
// char *hostName [IN]:
// Name of the remote host.
//
// Assumes: The function has been called correctly from the command line. Also
// assumes that server is already running and is blocking, waiting for
// data.
// Results: A UPD socket is created.
// Returns: The socket descriptor of the remote socket.
// ----------------------------------------------------------------------------
//
int CreateUDPSocket(struct sockaddr_in &remote, char *szHostName, char *szPort)
{
int nSocket;
hostent *hp; // Address of remote host (server).

//
// Create the socket.
//
if ((nSocket = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
perror("Error creating socket in CreateUDPSocket");
exit(1);
}

//
// Get the address of the remote host.
//
hp = gethostbyname(szHostName);

//
// Set up the remote address.
//
remote.sin_family = AF_INET;
memcpy(&remote.sin_addr, hp->h_addr_list[0], hp->h_length);
remote.sin_port = atoi(szPort);

return nSocket;
}


Jon Sagara

"After all is said and done, usually more is said than done." -- <i>Unknown</i>
Generalserver.cpp Pin
Jon Sagara13-Nov-01 17:58
Jon Sagara13-Nov-01 17:58 
GeneralRe: Sockets problem - figured it out Pin
Jon Sagara13-Nov-01 20:44
Jon Sagara13-Nov-01 20:44 
GeneralRe: Sockets problem - figured it out Pin
Nish Nishant13-Nov-01 21:45
sitebuilderNish Nishant13-Nov-01 21:45 
GeneralRe: Sockets problem - figured it out Pin
Jon Sagara13-Nov-01 21:54
Jon Sagara13-Nov-01 21:54 
GeneralRe: Sockets problem - figured it out Pin
Jon Sagara13-Nov-01 21:57
Jon Sagara13-Nov-01 21:57 
GeneralRe: Sockets problem - figured it out Pin
Nish Nishant13-Nov-01 22:19
sitebuilderNish Nishant13-Nov-01 22:19 
QuestionHow to Browse for a Directory? Pin
Jerry Wang13-Nov-01 14:20
Jerry Wang13-Nov-01 14:20 
AnswerRe: How to Browse for a Directory? Pin
Michael Dunn13-Nov-01 16:09
sitebuilderMichael Dunn13-Nov-01 16:09 
GeneralRe: How to Browse for a Directory? Pin
PJ Arends13-Nov-01 17:13
professionalPJ Arends13-Nov-01 17:13 
GeneralRe: How to Browse for a Directory? Pin
Michael Dunn13-Nov-01 20:18
sitebuilderMichael Dunn13-Nov-01 20:18 
GeneralRe: How to Browse for a Directory? Pin
Mark Terrano16-Nov-01 12:24
Mark Terrano16-Nov-01 12:24 
Generala base question Pin
13-Nov-01 13:42
suss13-Nov-01 13:42 
GeneralSHGetPathFromID under WinXP vs Win2000 Pin
Mark Terrano13-Nov-01 12:05
Mark Terrano13-Nov-01 12:05 
Generalhardware access Pin
13-Nov-01 10:52
suss13-Nov-01 10:52 
GeneralRe: hardware access Pin
Mark Terrano16-Nov-01 12:21
Mark Terrano16-Nov-01 12:21 
GeneralIterate through registry key Pin
Christian Graus13-Nov-01 9:57
protectorChristian Graus13-Nov-01 9:57 
GeneralRe: Iterate through registry key Pin
Tomasz Sowinski13-Nov-01 10:13
Tomasz Sowinski13-Nov-01 10:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.