Java Socket Problem

Hi,

I have got a problem with sending information through a Java socket to a Python Sever... For some reason the string variable that I said through which is a discID never gets sent.

I know the server is not the problem because I create a Python Client to test it...

Both The sever and client are running on local host and they bind with each other, I have checked this by doing the clientSocket.getConnected method and it returns true.

If you could help me out it would be really helpful.

Thanks

Here the code:

publicclass ServerConnection

{

private String serverAddress ="127.0.0.1";

privateint serverPort = 5555;

private Socket clientSocket =null;

private PrintWriter informationOut =null;

private BufferedReader informationIn =null;

private String unFormattedInfo;

private String discHash;

public ServerConnection(){

discHash = DiscInfo.findCdInformation();

serverConnect();

}

publicvoid serverConnect()

{

try{

System.out.println("Connecting To Server...");

clientSocket =new Socket(serverAddress, serverPort);

informationOut =new PrintWriter(clientSocket.getOutputStream(),true);

informationIn =new BufferedReader(new InputStreamReader(clientSocket

.getInputStream()));

}

catch (UnknownHostException e){

System.err.println("Don't Know Host: " + clientSocket.getInetAddress());

System.exit(1);

}

catch (IOException e){

System.err.println("I/O Error While Connecting To Host: ");

System.exit(1);

}

BufferedReader bufferInput =new BufferedReader(new InputStreamReader(

System.in));

try{

while ((discHash = bufferInput.readLine()) !=null){

informationOut.println(discHash);

unFormattedInfo = informationIn.readLine();

}

informationOut.close();

informationIn.close();

bufferInput.close();

clientSocket.close();

}

catch (IOException error){

// TODO Auto-generated catch block

error.printStackTrace();

}

}

/**

* @return This serverConnection's unFormattedInfo

*/

public String getUnFormattedInfo(){

return unFormattedInfo;

}

}

[4070 byte] By [ChrisUKa] at [2007-11-27 2:09:43]
# 1
>while ((discHash = bufferInput.readLine()) != null) {>informationOut.println(discHash);>unFormattedInfo = informationIn.readLine();>}have you entered some data after running ServerConnection ?
java_2006a at 2007-7-12 2:00:30 > top of Java-index,Java Essentials,New To Java...
# 2
In general it's not always that easy to make sure that data going through sockets has been sent. Using a PrintStream and calling everything with println helps. Otherwise flushing in a paranoid way can also help.
tjacobs01a at 2007-7-12 2:00:30 > top of Java-index,Java Essentials,New To Java...
# 3
Also make sure the data you're sending actually ends with a newline character, or else readline() will sit there forever.EDIT: Nevermind, I see now that you're using PrintWriter so that shouldn't be the problem.Message was edited by: BinaryDigit
BinaryDigita at 2007-7-12 2:00:30 > top of Java-index,Java Essentials,New To Java...
# 4

Well the data for discHash is already inputted before the connection is made...

Im really stuck.... here the python code:

import socket

import os

import sys

import re

host = 'localhost'

port = 5555

backlog = 5

size = 4096

baseDirectory ='/Users/Chris/Desktop/freedb/'

albumTitle = re.compile('DTITLE=(.*)')

offset = re.compile('#\s+(\d+)')

s = None

try:

s = cdSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

s = cdSocket.bind((host,port))

s = cdSocket.listen(backlog)

except socket.error, (value,message):

if s:

s.close()

print 'Could Not Open Socket: '+ message

sys.exit(1)

while 1:

clientSocket, address = cdSocket.accept()

hash = clientSocket.recv(size)

print "Hello "+hash

client = clientSocket.makefile('rw', 0)

genre_dirs = []

for name in os.listdir(baseDirectory):

directoryPath = os.path.join(baseDirectory, name)

if os.path.isdir(directoryPath):

genre_dirs.append(directoryPath)

for genre_dir in genre_dirs:

idFilePath = os.path.join(genre_dir, hash)

if os.path.exists(idFilePath):

cdInfoFilePath = open(idFilePath)

cdInfoFileRead = cdInfoFilePath.read(size)

clientSocket.send(cdInfoFileRead)

cdInfoFilePath.close()

clientSocket.close()

Message was edited by:

ChrisUK

ChrisUKa at 2007-7-12 2:00:30 > top of Java-index,Java Essentials,New To Java...
# 5

I'm suspicious of code that writes byte arrays but tries to read lines. Whatever you do should be the same on both ends.

Does the cdInfoFile contain any newlines? And why are you only sending the first 4096 bytes of each cdInfoFile? Do these files really have a newline as the 4096th byte in every case? If not, how can it work?

ejpa at 2007-7-12 2:00:30 > top of Java-index,Java Essentials,New To Java...