View Full Version : Sending Internet Events from Java and C#
Ron
October 13th, 2002, 12:55 PM
Cool! Can I include these examples in the documentation, with your name of course ?
Ron
October 13th, 2002, 12:55 PM
To keep the formatting put code bb-tags around the code. And thanks for the files. I'll upload them to the misc. section of the download page.
<font size=-1>[ This Message was edited by: RonB on 2002-03-29 23:12 ]</font>
edanuff
October 13th, 2002, 12:55 PM
I'm posting simple example code to send an internet event from Java or C#. Useful if you are building web-based UI's that communicate to Girder.
edanuff
October 13th, 2002, 12:55 PM
/*
* GirderInternetEventClient.java
*
*/
import java.util.*;
import java.io.*;
import java.security.*;
import java.net.*;
public class GirderInternetEventClient {
public GirderInternetEventClient() {
}
public static void main(String[] args) {
try {
if (args.length == 4) {
sendEventString(args[0], Integer.parseInt(args[1]), args[2], args[3]);
return;
}
System.out.println("Girder Internet Event Client");
System.out.print("Enter host [localhost]: ");
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
String host = input.readLine();
if (host.equals("")) host = "localhost";
System.out.print("Enter port [1024]: ");
String port = input.readLine();
if (port.equals("")) port = "1024";
System.out.print("Enter password [NewDefPWD]: ");
String password = input.readLine();
if (password.equals("")) password = "NewDefPWD";
System.out.print("Enter event [CR ends]: ");
String event_string = input.readLine();
while (!event_string.equals(""))
{
sendEventString(host, Integer.parseInt(port), password, event_string);
System.out.print("Enter event [CR ends]: ");
event_string = input.readLine();
}
System.out.println("Done.");
}
catch (Exception e) {
System.out.println(e);
e.printStackTrace();
}
}
public static byte[] calcMD5(byte[] data, int len) {
if (data != null) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
if (md != null) {
md.update(data, 0, len);
return md.digest();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
public static String byteArrayToHexString(byte[] bytes) {
StringBuffer hex_string = new StringBuffer(bytes.length * 2);
if (bytes != null) {
for (int i = 0; i < bytes.length; i++) {
int byte_val = bytes[i];
if (byte_val < 0) byte_val += 256;
String hex_code = Integer.toHexString(byte_val);
if ((hex_code.length() % 2) == 1) hex_code = "0" + hex_code;
hex_string.append(hex_code);
}
}
return hex_string.toString();
}
public static String getMD5String(String str) {
byte[] bytes = str.getBytes();
return byteArrayToHexString(calcMD5(bytes, bytes.length));
}
public static void sendEventString(String hostname, int port, String password, String event_string) {
Socket socket = null;
try {
socket = new Socket(hostname, port);
BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter output = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true);
output.println("quintessence");
String cookie = input.readLine();
output.println(getMD5String(cookie + ":" + password));
String accept_result = input.readLine();
if (accept_result.equalsIgnoreCase("accept")) {
output.println(event_string);
output.println("close");
}
}
catch (Exception e) {
}
finally {
try {
if (socket != null) socket.close();
}
catch (Exception e) {
}
}
}
}
edanuff
October 13th, 2002, 12:55 PM
using System;
using System.Security.Cryptography;
using System.Text;
using System.Net.Sockets;
using System.IO;
namespace GirderInternetEventClient
{
public class GirderInternetEventClient
{
public GirderInternetEventClient()
{
}
public static void Main(string[] args)
{
if (args.Length == 4)
{
sendEventString(args[0], int.Parse(args[1]), args[2], args[3]);
return;
}
Console.WriteLine("Girder Internet Event Client");
Console.Write("Enter host [localhost]: ");
string host = Console.ReadLine();
if (host.Equals("")) host = "localhost";
Console.Write("Enter port [1024]: ");
string port = Console.ReadLine();
if (port.Equals("")) port = "1024";
Console.Write("Enter password [NewDefPWD]: ");
string password = Console.ReadLine();
if (password.Equals("")) password = "NewDefPWD";
Console.Write("Enter event [CR ends]: ");
string event_string = Console.ReadLine();
while (!event_string.Equals(""))
{
sendEventString(host, int.Parse(port), password, event_string);
Console.Write("Enter event [CR ends]: ");
event_string = Console.ReadLine();
}
Console.WriteLine("Done");
}
public static String getHexString(byte[] bytes)
{
String hex_string = "";
byte[] item = { 0 };
for (int i = 0; i < bytes.Length; i++)
{
item[0] = bytes[i];
hex_string += BitConverter.ToString(item);
}
return hex_string;
}
public static String getMD5String(String str)
{
MD5 md5 = new MD5CryptoServiceProvider();
byte[] result = md5.ComputeHash((new UTF8Encoding()).GetBytes(str));
return getHexString(result);
}
public static void sendEventString(String hostname, int port, String password, String event_string)
{
TcpClient tcpClient = new TcpClient();
try
{
tcpClient.Connect(hostname, port);
NetworkStream networkStream = tcpClient.GetStream();
StreamReader input = new StreamReader(networkStream);
StreamWriter output = new StreamWriter(networkStream);
output.AutoFlush = true;
output.WriteLine("quintessence");
String cookie = input.ReadLine();
output.WriteLine(getMD5String(cookie + ":" + password));
String accept_result = input.ReadLine();
if (accept_result.Equals("accept"))
{
output.WriteLine(event_string);
output.WriteLine("close");
}
}
finally
{
tcpClient.Close();
}
}
}
}
edanuff
October 13th, 2002, 12:55 PM
Sure. I just emailed the files to you, since the discussion board removed the formatting.
Ed
Powered by vBulletin® Version 4.1.8 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.