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

C / C++ / MFC

 
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 
Generalserver.cpp Pin
Jon Sagara13-Nov-01 17:58
Jon Sagara13-Nov-01 17:58 
// ============================================================================
// server.cc
//
// This program is responsible for accepting requests for files from the rcopy
// program and transmitting the file back. This program should never terminate
// (unless you kill it with a ctrl-c). It should continually process requests
// from the rcopy program.
//
// The server needs to handle error conditions such as non-existing files by
// passing back a flag to the rcopy program and then waiting for a new file
// request.
//
// The server should output its port number to be used by the rcopy program.
// The server program will be run as:
//
// server error-percent
//
// where
//
// error-percent is the percent of packets that are in error (floating
// point number)
//
// Author: Jon Sagara
//
// This file represents the SERVER.
// ============================================================================

#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 CreateServerSocket();


int main(int argc, char *argv[])
{
int nSocket;
int nBytesRead = 0; // Number of bytes read from rcopy.
int nBytesSent = 0; // Number of bytes send to rcopy.
int nRemoteLen = 0; // Length of remote

sockaddr_in remote; // Socket address of rcopy.
unsigned char recv_buf[BUFF_SIZE];
unsigned char send_buf[BUFF_SIZE];

//
// Check command line arguments.
//
if (2 != argc)
{
cout << "usage: " << argv[0] << " error-percent" << endl;
exit(-1);
}

nSocket = CreateServerSocket();
nBytesRead = recvfrom(nSocket, recv_buf, BUFF_SIZE, 0, (struct sockaddr *) &remote, &nRemoteLen);

cout << recv_buf << " len: " << nBytesRead << endl;

memcpy(send_buf, "Jon Sagara", 11);
nBytesSent = sendto(nSocket, send_buf, 11, 0, (struct sockaddr *) &remote, sizeof(remote));
if (nBytesSent < 0)
{
perror("Error sending data in server. Sending back to rcopy");
close(nSocket);
exit(1);
}

close(nSocket);

return 0;
}


// ----------------------------------------------------------------------------
// CreateServerSocket()
//
// Creates a server socket and binds it to a particular address and port. This
// allows any process to (theoretically) connect to our server.
//
// Assumes: We will bind to the local address.
// Results: A socket is created, bound to the local machine's address.
// Returns: The socket descriptor.
// ----------------------------------------------------------------------------
//
int CreateServerSocket()
{
int nSocket = 0; // Socket descriptor.
int nLocalLen = 0; // sizeof(local).
sockaddr_in local; // Local socket address.

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

//
// Set up the socket ("name it").
//
local.sin_family = AF_INET; // Internet family.
local.sin_addr.s_addr = INADDR_ANY; // Wild card machine address (local address).
local.sin_port = 0; // Let the system choose the port.

//
// Bind the name (address) to a port.
//
nLocalLen = sizeof(local);
if ((bind(nSocket, (struct sockaddr *) &local, nLocalLen)) < 0)
{
perror("Error calling bind() in CreateServerSocket()");
exit(1);
}

//
// Get the port name and print it out.
//
getsockname(nSocket, (struct sockaddr *) &local, &nLocalLen);
cout << "Socket has port " << local.sin_port << "\n";

return nSocket;
}



Jon Sagara

"After all is said and done, usually more is said than done." -- <i>Unknown</i>
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 
GeneralCreating a DIB from HANDLE not DDB Pin
Chambers13-Nov-01 7:55
Chambers13-Nov-01 7:55 

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.