File: nettut.cpp

package info (click to toggle)
crystalspace 0.94-20020412-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 62,276 kB
  • ctags: 52,843
  • sloc: cpp: 274,783; ansic: 6,608; perl: 6,276; objc: 3,952; asm: 2,942; python: 2,354; php: 542; pascal: 530; sh: 430; makefile: 370; awk: 193
file content (161 lines) | stat: -rw-r--r-- 3,128 bytes parent folder | download
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
    This program demonstrates very basic TCP usage of the ENSOCKET plugin
*/

#include <stdio.h>
#include "cssysdef.h"
#include "csutil/scf.h"
#include "csutil/cfgfile.h"

#include "inetwork/driver2.h"
#include "inetwork/socket2.h"
#include "inetwork/sockerr.h"


CS_IMPLEMENT_APPLICATION

iNetworkDriver2 *driver = NULL;
iNetworkSocket2 *server = NULL;
iNetworkSocket2 *client = NULL;

void usage ( char *arg )
{
	printf("Usage:  %s [server/client] [hostname*] [port]\n",arg);
	printf(" * - specify hostname only if you are using client mode.\n");
	
}

int main(int argc, char *argv[])
{

  int role = -1;  // 0 - server 1 - client
  
  char hostname[65]; // RFCs say that DNS hostnames are not longer than 64 bytes...
  int port = 0;
  char buffer[1024];

  if (argc < 3)
  {
	  usage(argv[0]);
	  return 0;
  }

  if (strcasecmp(argv[1],"server") == 0)
  {
	  role = 0;
  }
  if (strcasecmp(argv[1],"client") == 0)
  {
	  role = 1;
  }

  if (role == -1)
  {
	  printf("you must specify client or server\n");
	  usage(argv[0]);
	  return 0;
  }

  if (role == 0) 
  {
      port = atoi(argv[2]);
  } else {
	  if (argc < 4) {
		  printf("please specify a hostname and port\n");
		  usage(argv[0]);
		  return 0;
	  }

	  strncpy(hostname,argv[2],64); // so we dont fill our buffer
	  port = atoi(argv[3]);
  }

  if (port == 0)
  {
	  printf("you must specify a port\n");
	  usage(argv[0]);
	  return 0;
  }

  if (role == 0) 
  {
	printf("server running tcp on port %d.\n",port);
  } else {
	printf("server running udp on port %d.\n",port);
  }
  
  
  // This method requires you register dlls with scfreg (or manually) in scf.cfg
  csConfigFile config ("scf.cfg");
  scfInitialize (&config);

  driver = SCF_CREATE_INSTANCE("crystalspace.network.driver.sockets2", iNetworkDriver2);
  if (!driver)
  {
	  printf("Unable to load the network plugin.\n");
	  return 0;
  }


  if (role == 0)
  {
	// act as a server
	server = driver->CreateSocket(CS_NET_SOCKET_TYPE_TCP);
	if (server == NULL)
	{
	  printf("unable to create socket\n");
	  return 0;
	}

	server->WaitForConnection(0,port,5);
	if (server->LastError() != CS_NET_SOCKET_NOERROR)
	{
		printf("unable to bind to port %d\n",port);
		return 0;
	}
	
	printf("waiting for connection on port %d...",port);
    
	client = server->Accept();
	if (client == NULL) 
	{
	  printf("unable to accept connection\n");
	  return 0;
	}
	printf("client connection from %s\n",client->RemoteName());
	client->Recv(buffer,1024);
	
	printf("I got '%s' from the client.\n",buffer);
	client->Close();
    
	server->DecRef();
	return 0;

  } else {
	// act as a client
	client = driver->CreateSocket(CS_NET_SOCKET_TYPE_TCP);
	if (client == NULL)
	{
	  printf("unable to create socket\n");
	  return 0;
	}

	printf("connecting to %s port %d...",hostname,port);
	client->Connect(hostname,port);
	if (client->IsConnected() == FALSE)
	{
	  printf("unable to resolve %s\n",hostname);
	  return 0;
	}
	sprintf(buffer,"CrystalSpace Network Tutorial Using ENSOCKET Plugin!");
	client->Send(buffer,strlen(buffer));
	client->Close();
	client->DecRef();

  }

  
  driver->DecRef();

  return 0;
}