Example #1
0
// Send the various Initialisation commands.  These include :
//  - HELLO
//  - NO_FOLLOW
//  - CMD_GET_LATEST_MDG_ID
// Return 0 if the socket is still valid.  Return -1 if the 
// socket closed or there was an error while sending.
int sendInit(int handle)
{
	if (handle < 0) { return -1; }
	
	// the hello command will be generated and placed in a buffer.  1024 bytes is PLENTY for this 
	// command... but just showing that the buffer doesn't need to be the exact size, just as long 
	// as it is big enough.
	char buffer[1024];
	
	// to authenticate, we simply must provide the proper HELLO string.  
	char hello_str[] = "RISP Server";
	
	risp_length_t len = 0;
	len += risp_addbuf_str(    buffer+len, CMD_HELLO, strlen(hello_str), hello_str);
	len += risp_addbuf_noparam(buffer+len, CMD_NOFOLLOW);
	len += risp_addbuf_noparam(buffer+len, CMD_GET_LATEST_MDG_ID);
	
	// now that the we have the RISP stream created for the command, we need to send it.
	return(sock_send(handle, buffer, len));
}
Example #2
0
// send the GOODBYE command to the server.  Return 0 if the socket is still valid.  Return -1 if the 
// socket closed or there was an error while sending.
int sendGoodbye(int handle)
{
	// the command will be generated and placed in a buffer.  1024 bytes is PLENTY for this 
	// command... but just showing that the buffer doesn't need to be the exact size, just as long 
	// as it is big enough.
	char buffer[1024];
	
	risp_length_t len = risp_addbuf_noparam(buffer, CMD_GOODBYE);
	assert(len > 0);
	
	// now that the we have the RISP stream created for the command, we need to send it.
	return(sock_send(handle, buffer, len));
}
Example #3
0
int main(int argc, char **argv)
{
	// parameters that are provided.
	char *srv = "127.0.0.1";
	int port = DEFAULT_PORT;
	char *name = NULL;
	char *message = NULL;

	int c;
	while ((c = getopt(argc, argv, 
		"s:" /* server hostname or ip */
		"p:" /* port to connect to */
		"n:" /* name of the message sender */
		"m:" /* message to send */
		"h"  /* command usage */
	)) != -1) {
		switch (c) {
			case 'p':
				port = atoi(optarg);
				assert(port > 0);
				break;
			case 's':
				srv = strdup(optarg);	// this will allocate some memory, but we will not be clearing it. When the application exits, it will clear by default.
				assert(srv);
				break;
			case 'n':
				name = strdup(optarg);
				assert(name);
				break;
			case 'm':
				message = strdup(optarg);
				assert(message);
				break;
			case 'h':
				printf("Usage: ./risp_chat_send -s [server] -p [port] -n \"name of sender\" -m \"Message\"\n\n");
				exit(1);
				break;
			default:
				fprintf(stderr, "Illegal argument \"%c\"\n", c);
				return 1;
		}
	}

	// set the signal trap.
	signal(SIGINT, sig_handler);
	
	// connect to the remote socket, and set it to non-blocking.
	assert(srv && port > 0);
	int handle = sock_connect(srv, port);
	if (handle <= 0) {
		printf("Unable to connect to %s:%d\n", srv, port);
	}
	else {

// 		printf("Connected to server.\n");
		
		// to simplify this process, we will join all the commands together.  We will not wait for responses.

		int len = 0;	// this var will have the length of each addition to the buffer.
		int buflen = 0;
		assert(message);
		int bufmax = 1024 + strlen(message);
		if (name) { bufmax += strlen(name); }
		char *buffer = malloc(bufmax);
		assert(buffer);
	
		// to authenticate, we simply must provide the proper HELLO string.  
		char hello_str[] = "RISP Server";
		len = risp_addbuf_str(buffer, CMD_HELLO, strlen(hello_str), hello_str);
		assert(len > 0);
		buflen += len;
		assert(buflen <= bufmax);

		// set the session in NO ECHO mode.
		len = risp_addbuf_noparam(buffer+buflen, CMD_NOECHO);
		assert(len > 0);
		buflen += len;
		assert(buflen <= bufmax);

		// set the session in NO FOLLW mode.
		len = risp_addbuf_noparam(buffer+buflen, CMD_NOFOLLOW);
		assert(len > 0);
		buflen += len;
		assert(buflen <= bufmax);

		// set the session in NO UPDATE mode.
		len = risp_addbuf_noparam(buffer+buflen, CMD_NOUPDATE);
		assert(len > 0);
		buflen += len;
		assert(buflen <= bufmax);

		// if we have a 'name' specified, then set that.
		if (name) {
			len = risp_addbuf_str(buffer+buflen, CMD_NAME, strlen(name), name);
			assert(len > 0);
			buflen += len;
			assert(buflen <= bufmax);
		}
	
		// now add the message
		assert(message);
		len = risp_addbuf_str(buffer+buflen, CMD_MESSAGE, strlen(message), message);
		assert(len > 0);
		buflen += len;
		assert(buflen <= bufmax);
		
		// and finally, tell the server to close the connection once it is finished.
		len = risp_addbuf_noparam(buffer+buflen, CMD_GOODBYE);
		assert(len > 0);
		buflen += len;
		assert(buflen <= bufmax);
		
// 		printf("Sending %d bytes to server.\n", buflen);
		
		// now that the we have the RISP stream created for the command, we need to send it.
		if (sock_send(handle, buffer, buflen) != 0) {
			// couldn't send the command, close the handle, and exit.
			close(handle);
			handle = -1;
		}
		else {

			// now we simply process the socket until it closes.  We dont actually care about processing the data received.
			while (recv(handle, buffer, bufmax, 0) != 0) {
// 				printf("Waiting...\n");
			}
		
			close(handle);
		}
	}

	
	return 0;
}