C++ Help <3

Garzilla04

Limp Gawd
Joined
Oct 26, 2003
Messages
226
I am in the process of writing a C++ program for my distributed systems class. Im running into a bit of trouble though. Our basic task is to have two separate programs that communicate between eachother (Client and Server).

What Im doing is developing a Netflix-ish ordering system. Basically our client side of the program will offer three options:

1. View top 10 movies
2. Place Order
3. Exit

Option one being a simple output of the Top 10 movies for the customer to see. Thats all set and is coded into our client side of the program. Option 2 is where it gets hard. We want it to simple recognize a number key entry 1-0 with each of the numbers representing a movie.

We want the request to be received by our server, which will be waiting for an incoming request. All our server end has to do is simply respond back to the client with an "Order recevied" message along with a random Order number.

My question to [H] is..how can I get the client to send out to the server program, have the server recognize the number entry (1-0) and simply send back to the client side a message along these lines "Thank you for ordering *INSERT MOVIE TITLE HERE*, here is your tracking number *INSERT RANDOM NUMBER*.

If anyone that can provide me with some working code or some advice on how to do it...I would love you forever. Its possible there could be some money involved via paypal for some elaborate help. I can provide all code we have so far.

I need help asap on this..as its due tomorrow night :D

Contact me on AIM: Matix32104
 
Just use sockets

There are plenty of abstractions to the winsock api that make it very easy to use available on the net
 
Basic guts of a blocking socket for windows. (must link to wsock32.lib)
Code:
#include <winsock.h>
#include <stdio.h>

int main()
{
	WSADATA		WSAData;
	SOCKET		sock;
	SOCKADDR_IN	sa;
	char		sendMsg[] = "GET /index.html HTTP/1.0\r\n\r\n";
	char		recvMsg[4096] = "No data recieved.";

	WSAStartup(MAKEWORD(2,0), &WSAData);
	sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

	sa.sin_family		= AF_INET;
	sa.sin_port		= htons(80);
	sa.sin_addr.S_un.S_addr	= inet_addr("64.233.179.104"); // google.com

	connect(sock, (SOCKADDR *) &sa, sizeof(SOCKADDR_IN));
	send(sock, sendMsg, sizeof(sendMsg), 0);
	recv(sock, recvMsg, sizeof(recvMsg), 0);
	closesocket(sock);
	WSACleanup();
	printf("%s", recvMsg);
	return 0;
}
 
And here's a server
Code:
#include <windows.h>
#include <winsock.h>
#include <stdio.h>

int main()
{
	WSADATA		WSAData;
	SOCKET		sock, client;
	SOCKADDR_IN	sa;
	char		sendMsg[] = "HTTP/1.0 200 OK\nContent-type: text/html\nContent-Length: 23\n\n<h3>Hello, world!</h3>\n";
	char		recvMsg[4096] = "No data recieved.";

	WSAStartup(MAKEWORD(2,0), &WSAData);
	sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

	sa.sin_family		= AF_INET;
	sa.sin_port		= htons(80);
	sa.sin_addr.S_un.S_addr	= inet_addr("127.0.0.1");
	bind( sock, (SOCKADDR *) &sa, sizeof(SOCKADDR_IN));
	listen(sock, 1);

	printf("Waiting for client to connect...\n");
	client = SOCKET_ERROR;
	while( client == SOCKET_ERROR )
	{
		Sleep(1);
		client = accept( sock, NULL, NULL );
	}
	printf("Client connected.\n");
	recv(client, recvMsg, sizeof(recvMsg), 0);
	printf("<-%s", recvMsg);
	printf("->%s", sendMsg);
	send(client, sendMsg, sizeof(sendMsg), 0);
	Sleep(5000);
	closesocket(client);
	WSACleanup();
	return 0;
}

(I'm bored.)
 
I probably already helped you too much already. Feel free to ask for help if you get into a specific problem.
 
yea hey some probs.

Server / client wont talk to each other :(

here's the code for each


Client

Code:
#include <winsock.h>
#include <stdio.h>
int main()
{
	WSADATA		WSAData;
	SOCKET		sock;
	SOCKADDR_IN	sa;
	char		sendMsg[] = "hi";
	char		recvMsg[4096] = "No data recieved.";

	WSAStartup(MAKEWORD(2,0), &WSAData);
	sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

	sa.sin_family		= AF_INET;
	sa.sin_port		= htons(80);
	sa.sin_addr.S_un.S_addr	= inet_addr("127.0.0.1"); // no stop it

	connect(sock, (SOCKADDR *) &sa, sizeof(SOCKADDR_IN));
	send(sock, sendMsg, sizeof(sendMsg), 0);
	recv(sock, recvMsg, sizeof(recvMsg), 0);
	closesocket(sock);
	WSACleanup();
	printf("%s", recvMsg);
	return 0;

Server

Code:
#include <windows.h>
#include <winsock.h>
#include <stdio.h>
int main()
{
	WSADATA		WSAData;
	SOCKET		sock, client;
	SOCKADDR_IN	sa;
	char		sendMsg[] = "Hi";
	char		recvMsg[4096] = "No data recieved.";

	WSAStartup(MAKEWORD(2,0), &WSAData);
	sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

	sa.sin_family		= AF_INET;
	sa.sin_port		= htons(80);
	sa.sin_addr.S_un.S_addr	= inet_addr("127.0.0.1");
	bind( sock, (SOCKADDR *) &sa, sizeof(SOCKADDR_IN));
	listen(sock, 1);

	printf("Waiting for client to connect...\n");
	client = SOCKET_ERROR;
	while( client == SOCKET_ERROR )
	{
		Sleep(1);
		client = accept( sock, NULL, NULL );
	}
	printf("Client connected.\n");
	recv(client, recvMsg, sizeof(recvMsg), 0);
	printf("<-%s", recvMsg);
	printf("->%s", sendMsg);
	send(client, sendMsg, sizeof(sendMsg), 0);
	Sleep(5000);
	closesocket(client);
	WSACleanup();
	return 0;
}
 
Make sure nothing is already using port 80 and no software firewall is blocking the program from getting it. I tested the code on my end and it works. All those functions will return certain values on success, you can go to msdn.com and search for those functions to ensure there isnt a bork in the pipeline.
 
yea changed the port to 100 works now :D

Now I just have to figure out a way for it to make decisions, ummm so could i basically have recvmsg in an if statement? so we can do press 1 to do this and it could do an if statement or a switch?

yea our professor doesnt teach us shit
 
Go talk to your teacher's assistant, I dont think they want you using sockets if you have questions about parsing a string. ;o)
 
Lord of Shadows said:
Go talk to your teacher's assistant, I dont think they want you using sockets if you have questions about parsing a string. ;o)


right now we just need it to work and if we can get this to work it'll pwn.

he told us doesn't matter how it works, as long as it works man. But you've been a huge ass help so far.
 
You can learn a lot about this shit just reading the posts, too bad i'm tired and not really retaining anything. I could have used this about 2 months ago, but didnt think to post here about it. Should have though.
 
Back
Top