[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Bug-gnubg] Multiprocessing
From: |
Nardy Pillards |
Subject: |
Re: [Bug-gnubg] Multiprocessing |
Date: |
Wed, 9 Jul 2003 02:23:08 +0200 |
Example of sockets and Win32
(from http://web.presby.edu/~wasmith/courses/315/samples/sockets/ )
// file: mpclient.cpp
// author: Wayne Smith
// date: March 2003
// To compile this on Solaris (cs2), try:
// g++ mpclient.cc -o mpclient -lsocket -lnsl
// To compile this on Darwin/MacOS X (titanium), try:
// c++ mpclient.cc -o mpclient
// To compile this on Windows, try:
// cl mpclient.cc -GX -DWIN32 ws2_32.lib
#include <iostream>
#include <string>
#include <fstream>
#ifdef WIN32
#include <windows.h> // this includes winsock2 (among many other things)
#include <io.h> // needed for read, write
#else
#include <sys/socket.h> // basic socket definitions
#include <netinet/in.h> // sockaddr_in and other Internet defns
#include <arpa/inet.h> // inet(3) functions
#include <unistd.h> // needed for read, write
#include <strings.h> // for bzero
#include <stdlib.h> // for atoi
#endif
using namespace std;
bool readLine(int sock, string & buf);
int main(int argc, char **argv) {
int sockfd, n;
struct sockaddr_in servaddr;
#ifdef WIN32
WSADATA wsaData;
int Ret;
#endif
// process command-line args
if (argc < 3) {
cerr << "usage: " << argv[0] << " serverFileName localFileName "
<< "[IPaddress] [serverPort]" << endl;
return -1;
}
char *serverFileName = argv[1],
*localFileName = argv[2],
*ipaddress = "10.16.1.20";
int portnum = 4004;
if (argc > 3) ipaddress = argv[3];
if (argc > 4) portnum = atoi(argv[4]);
// initialize the winsock subsystem
#ifdef WIN32
if ((Ret = WSAStartup(MAKEWORD(2,2), &wsaData)) != 0) {
cerr << "winsock startup error" << endl;
return -1;
}
#endif
// do the socket/connect thing as a client
// winsock note: checking for < 0 may not be best because
// socket returns unsigned; try == INVALID_SOCKET
// (where INVALID_SOCKET defined in winsock2.h)
if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
cerr << "socket error" << endl;
#ifdef WIN32
WSACleanup();
#endif
return -1;
}
#ifdef WIN32
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(portnum);
servaddr.sin_addr.s_addr = inet_addr(ipaddress);
#else
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(portnum);
#endif
#ifdef sun
if (inet_pton(AF_INET, ipaddress, &servaddr.sin_addr) <= 0)
#endif
#ifdef __APPLE__
if (inet_aton(ipaddress, &servaddr.sin_addr) <= 0)
#endif
#ifndef WIN32
{
cerr << "inet_pton error for %s" << ipaddress << endl;
return -1;
}
#endif
// winsock example doesn't check < 0. Instead checks == SOCKET_ERROR
if (connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0)
{
cerr << "connect error" << endl;
#ifdef WIN32
closesocket(sockfd);
WSACleanup();
#else
close(sockfd);
#endif
return -1;
}
// create the file that will become a copy
ofstream myfile(localFileName);
if (!myfile) {
cerr << "cannot create " << localFileName << endl;
#ifdef WIN32
closesocket(sockfd);
WSACleanup();
#else
close(sockfd);
#endif
return -1;
}
// send a file name to the server to copy here
string msg = serverFileName;
msg += "\n";
#ifdef WIN32
// winsock apps typically use send (4th arg is flags)
if (send(sockfd, msg.c_str(), msg.length(), 0) == SOCKET_ERROR)
#else
if (write(sockfd, msg.c_str(), msg.length()) != msg.length())
#endif
{
cerr << "failure to write file name" << endl;
return -1;
}
// now read lines from the remote file and put in a file locally
int ctr = 0;
while (readLine(sockfd, msg)) {
myfile << msg << endl;
ctr++;
}
myfile.close();
if (ctr == 0) cerr << "Didn't read anything!" << endl;
else cout << "File copy complete." << endl;
#ifdef WIN32
closesocket(sockfd);
WSACleanup();
#else
close(sockfd);
#endif
return 0;
}
/*
Purpose: read a line of text from a socket
Output: a string containing the line without a newline at end
(newline is interpreted as "end of transmission")
*/
bool readLine(int sock, string & buf) {
char cbuf;
buf = ""; // initialize to the empty string
bool newlineread = false;
#ifdef WIN32
// winsock apps typically use recv (4th arg is flags)
// (note: recv is available on unix side as well)
while (!newlineread && recv(sock, &cbuf, 1, 0) > 0)
#else
while (!newlineread && read(sock, &cbuf, 1) > 0)
#endif
{
if (cbuf == '\n') newlineread = true; // quit if newline just read
else buf = buf + cbuf;
}
// if we didn't get a newline, we failed
return newlineread;
}
Nardy
- Re: [Bug-gnubg] Multiprocessing, (continued)
- Re: [Bug-gnubg] Multiprocessing, Olivier Baur, 2003/07/07
- Re: [Bug-gnubg] Multiprocessing, Øystein Johansen, 2003/07/08
- SV: [Bug-gnubg] Multiprocessing, Henrik Ravn, 2003/07/08
- Re: [Bug-gnubg] Multiprocessing, Joern Thyssen, 2003/07/08
- Re: [Bug-gnubg] Multiprocessing, Nardy Pillards, 2003/07/08
- Re: [Bug-gnubg] Multiprocessing, Nardy Pillards, 2003/07/08
- Re: [Bug-gnubg] Multiprocessing, Nardy Pillards, 2003/07/08
- Re: [Bug-gnubg] Multiprocessing, Olivier Baur, 2003/07/08
- Re: [Bug-gnubg] Multiprocessing, Joern Thyssen, 2003/07/08
- Re: [Bug-gnubg] Multiprocessing, Nardy Pillards, 2003/07/08
- Re: [Bug-gnubg] Multiprocessing,
Nardy Pillards <=
- [Bug-gnubg] Multiprocessing quickstart webpage and Mac OS X binary, Olivier Baur, 2003/07/08
- Re: Bug-gnubg] Winsock (Was: [Bug-gnubg] Multiprocessing), Olivier Baur, 2003/07/09
- Re: Bug-gnubg] Winsock (Was: [Bug-gnubg] Multiprocessing), Nardy Pillards, 2003/07/09
- Re: Bug-gnubg] Winsock (Was: [Bug-gnubg] Multiprocessing), Holger, 2003/07/10
- Re: Bug-gnubg] Winsock (Was: [Bug-gnubg] Multiprocessing), Nardy Pillards, 2003/07/10
- Re: Bug-gnubg] Winsock (Was: [Bug-gnubg] Multiprocessing), Holger, 2003/07/10
- Re: Bug-gnubg] Winsock (Was: [Bug-gnubg] Multiprocessing), Nardy Pillards, 2003/07/10
- Re: Bug-gnubg] Winsock (Was: [Bug-gnubg] Multiprocessing), Holger, 2003/07/11
- Re: Bug-gnubg] Winsock (Was: [Bug-gnubg] Multiprocessing), Holger, 2003/07/11
- Re: Bug-gnubg] Winsock (Was: [Bug-gnubg] Multiprocessing), Olivier Baur, 2003/07/11