Sockets?

Discussion of Common Lisp
Post Reply
lithos
Posts: 14
Joined: Tue Feb 02, 2010 4:11 pm

Sockets?

Post by lithos » Sun May 09, 2010 3:34 pm

Should I be able to connect Sockets between two languages relatively painlessly? Basically am I missing something small or something big?

Using lispworks http://www.lispworks.com/documentation/ ... .htm#14172 and this sample(java) http://zerioh.tripod.com/ressources/sockets.html

Both are using unicode, and I'm under the impression that sockets are somewhat standardized. However when I try to connect the Java client to the Lisp server I get a "connection refused" error(my intention), when I connect the lisp client to the java server the connection is accepted BUT I get an invalid header error "java.io.StreamCorruptedException: invalid stream header: 48656C6C". The later is pretty clear but it still means I have no idea of where to start.

nuntius
Posts: 538
Joined: Sat Aug 09, 2008 10:44 am
Location: Newton, MA

Re: Sockets?

Post by nuntius » Sun May 09, 2010 9:22 pm

In the Java code, the ObjectOutputStream and ObjectInputStream talk a special protocol. If you just printed from connection.getInputStream() then everything should be fine.

lithos
Posts: 14
Joined: Tue Feb 02, 2010 4:11 pm

Re: Sockets?

Post by lithos » Mon May 10, 2010 11:07 am

Whoops. I'm starting to get some stuff to transfer back and forth now, actually reading the real java documentation instead copy-paste from the internet.

I'll post here again eventually with something that works how I want it to.

lithos
Posts: 14
Joined: Tue Feb 02, 2010 4:11 pm

Re: Sockets?

Post by lithos » Wed May 26, 2010 3:12 pm

Narrowed down the client code that I can use. Project feel asleep for a bit and I started messing with it again today. Got to feel really silly when I realized that not having a newline character was messing with me :lol: .

Code: Select all

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;

public class TestSocket 
{
	public static void main(String[] args)
	{
		try {
			Socket theSocket = new Socket("localhost", 10244);   //start socket
			
			System.out.println("connected to " + theSocket.getInetAddress() 
					+ " on port " + theSocket.getPort() + " from port " 
					+ theSocket.getLocalPort() + " of " + theSocket.getLocalAddress());  //socket info
			
			
			BufferedReader userIn = new BufferedReader(new InputStreamReader(System.in));  //user input
			
			BufferedReader networkIn = new BufferedReader(new InputStreamReader (theSocket.getInputStream()));  //input from server.  stream chained with readers for convience
			
			
			while(true)
			{
				String theLine = userIn.readLine();
				theLine+="\n";  //server waits for newline
				//byte[] converted =  "hello-Æ-ƒ-\n".getBytes();  //range testing, slighty different(but accurate) on server.
				byte[] converted = theLine.getBytes();
				theSocket.getOutputStream().write(converted);  //write to the stream.  don't you get that naked feeling when using unchained streams in Java :D
				System.out.println("user out: " + theLine + "  array: " + converted.length);
				System.out.println("server in: " + networkIn.readLine());
			}
			
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}

Post Reply