Advertisement

05.12.2008 at 11:38AM PDT, ID: 23395507
[x]
Attachment Details

c programming

Asked by trajj2008 in C Programming Language, C++ Programming Language

I need help -- I need a simple client server game in c code- I am supposed to write it utilizing icmpStart Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
/* TCPecho.cpp - main, TCPecho */
 
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <winsock.h>
 
void	TCPecho(const char *, const char *);
void	errexit(const char *, ...);
SOCKET	connectTCP(const char *, const char *);
 
#define	LINELEN		 128
#define	WSVERS		MAKEWORD(2, 0)
 
/*------------------------------------------------------------------------
 * main - TCP client for ECHO service
 *------------------------------------------------------------------------
 */
void
main(int argc, char *argv[])
{
	char	*host = "localhost";	/* host to use if none supplied	*/
	char	*service = "echo";	/* default service name		*/
	WSADATA	wsadata;
 
	switch (argc) {
	case 1:
		host = "localhost";
		break;
	case 3:
		service = argv[2];
		/* FALL THROUGH */
	case 2:
		host = argv[1];
		break;
	default:
		fprintf(stderr, "usage: TCPecho [host [port]]\n");
		exit(1);
	}
	if (WSAStartup(WSVERS, &wsadata) != 0)
		errexit("WSAStartup failed\n");
	TCPecho(host, service);
	WSACleanup();
	exit(0);
}
 
/*------------------------------------------------------------------------
 * TCPecho - send input to ECHO service on specified host and print reply
 *------------------------------------------------------------------------
 */
void TCPecho(const char *host, const char *service)
{
	char	buf[LINELEN+1];		/* buffer for one line of text	*/
	SOCKET	s;			/* socket descriptor		*/
	int	cc, outchars, inchars;	/* characters counts		*/
	char request[64];
 
	s = connectTCP(host, service);
 
	while (fgets(buf, sizeof(buf), stdin)) {
		buf[LINELEN] = '\0';	// ensure line null-termination	
		/* If user hit's 'q', stop the client */
		if ( buf[0] == 'q' ) 
			break;
		outchars = strlen(buf);
		(void) send(s, buf, outchars, 0);
 
		// read it back 
		for (inchars = 0; inchars < outchars; inchars += cc) {
			cc = recv(s, &buf[inchars], outchars-inchars, 0);
			if (cc == SOCKET_ERROR)
				errexit("socket recv failed: %d\n",
					GetLastError());
		}
		fputs(buf, stdout);
	}
 
	closesocket(s);
}
[+][-]05.12.2008 at 11:42AM PDT, ID: 21549471

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.12.2008 at 12:05PM PDT, ID: 21549635

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.12.2008 at 12:17PM PDT, ID: 21549717

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: C Programming Language, C++ Programming Language
Sign Up Now!
Solution Provided By: Infinity08
Participating Experts: 2
Solution Grade: A
 
 
[+][-]05.12.2008 at 12:21PM PDT, ID: 21549759

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628