473,382 Members | 1,362 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Need Desperate Help...Java Sockets

hi Everyone
I am trying to create a server and client to send files from one side
to the other. I can send files from one side to the other using
bufferedinput/output streams but when trying to send normal messages
with printwriter my files being sent across are messed up. Empty jpg's
are being sent with wrong file sizes. My code as follows:
public class TCPServer
{
public static final int BUFFER_SIZE = 4096;
public static final int RCV_PORT = 45000;

public static void main(String[] args)
{
ServerSocket server;
Socket socket;
File file;
PrintWriter out;
BufferedReader in;
String line;

file = new File("C:Project");

try
{
server = new ServerSocket(RCV_PORT);
socket = server.accept();

OutputStream os = socket.getOutputStream();
InputStream is = socket.getInputStream();
out = new PrintWriter(os,true);
in = new BufferedReader(new InputStreamReader(is));

out.println("OK"); //WHEN I HAVE THIS LINE MY FILE BEING SEND IT
MESSED UP. BUT WITHOUT THIS THE FILE IS SENT PERFECTLY
SendFile(file, "test.JPG", os);
out.close();
in.close();
socket.close();
} catch (IOException e) { }

}

static void SendFile(File file, String filename, OutputStream os)
{
try
{
byte[] buffer = new byte[BUFFER_SIZE];
int count;
File nfile = new File(file,filename);
FileInputStream Fis = new FileInputStream(nfile);
BufferedInputStream ins = new BufferedInputStream(Fis);
BufferedOutputStream outs = new BufferedOutputStream(os);
while(true)
{
count = ins.read(buffer, 0, BUFFER_SIZE);
if (count <= 0)
break;
outs.write(buffer, 0, count);
System.out.println(buffer);
}
outs.flush();
outs.close();
ins.close();
} catch (IOException e) {System.out.println(e);}
}

}

CLIENT
---------

public class TCPClient
{
public static final int BUFFER_SIZE = 4096;
public static final String FILE_NAME = "WORK.jpg";
public static void main(String[] args)
{
int port;
String addr;
try
{
Socket socket = new Socket(addr,port);
OutputStream os = socket.getOutputStream();
InputStream is = socket.getInputStream();
PrintWriter out = new PrintWriter(os, true);
BufferedReader in = new BufferedReader(new InputStreamReader(is));

String line = in.readLine(); //WHEN I HAVE THIS CODE ON THE CLIENT
SIDE
if (line.compareTo("OK")!=0) //WITH RESPECT TO WHAT I HAVE ON THE
SERVER SIDE
return; //THE FILE BEING RECEIVED IS MESSED UP, OTHERWISE IS PERFECTLY
RECEIVED

RcvFile(is);

out.close();
in.close();
socket.close();
} catch (IOException e) { }

}

static void RcvFile(InputStream is)
{
try
{
BufferedInputStream ins = new BufferedInputStream(is);
BufferedOutputStream outs = new BufferedOutputStream(new
FileOutputStream(FILE_NAME));
byte[] buffer = new byte[BUFFER_SIZE];

while(true)
{
int count = ins.read(buffer, 0, BUFFER_SIZE);
if (count <= 0)
{
break;
}
outs.write(buffer, 0, count);
System.out.println(buffer);
}
outs.flush();
ins.close();
outs.close();
}
catch(IOException e) {}
}

}


Please let me know what i am doing wrong. From all the playing around
i have done it seems that once i bind and send anything via the
printwriter the Bufferedinputstream doesn't work properly.

Thanks a million

JatP
Jul 17 '05 #1
1 3278
Did you look at the line separator on client and server side?
The documentation of PrintWriter says

* The println() methods
* use the platform's own notion of line separator rather than the newline
* character.

See also the private void newLine() method in PrintWriter.

Stephan

__________________________________________
Stephan Wehner
Editor, Traffic Life: Passionate Tales and Exit Strategies
An anthology about our car culture and alternatives with
short stories, poems, cartoons and lots of other art
www.trafficlife.com

vi********@yahoo.com (JatP) wrote in message news:<8e**************************@posting.google. com>...
hi Everyone
I am trying to create a server and client to send files from one side
to the other. I can send files from one side to the other using
bufferedinput/output streams but when trying to send normal messages
with printwriter my files being sent across are messed up. Empty jpg's
are being sent with wrong file sizes. My code as follows:
public class TCPServer
{
public static final int BUFFER_SIZE = 4096;
public static final int RCV_PORT = 45000;

public static void main(String[] args)
{
ServerSocket server;
Socket socket;
File file;
PrintWriter out;
BufferedReader in;
String line;

file = new File("C:Project");

try
{
server = new ServerSocket(RCV_PORT);
socket = server.accept();

OutputStream os = socket.getOutputStream();
InputStream is = socket.getInputStream();
out = new PrintWriter(os,true);
in = new BufferedReader(new InputStreamReader(is));

out.println("OK"); //WHEN I HAVE THIS LINE MY FILE BEING SEND IT
MESSED UP. BUT WITHOUT THIS THE FILE IS SENT PERFECTLY
SendFile(file, "test.JPG", os);
out.close();
in.close();
socket.close();
} catch (IOException e) { }

}

static void SendFile(File file, String filename, OutputStream os)
{
try
{
byte[] buffer = new byte[BUFFER_SIZE];
int count;
File nfile = new File(file,filename);
FileInputStream Fis = new FileInputStream(nfile);
BufferedInputStream ins = new BufferedInputStream(Fis);
BufferedOutputStream outs = new BufferedOutputStream(os);
while(true)
{
count = ins.read(buffer, 0, BUFFER_SIZE);
if (count <= 0)
break;
outs.write(buffer, 0, count);
System.out.println(buffer);
}
outs.flush();
outs.close();
ins.close();
} catch (IOException e) {System.out.println(e);}
}

}

CLIENT
---------

public class TCPClient
{
public static final int BUFFER_SIZE = 4096;
public static final String FILE_NAME = "WORK.jpg";
public static void main(String[] args)
{
int port;
String addr;
try
{
Socket socket = new Socket(addr,port);
OutputStream os = socket.getOutputStream();
InputStream is = socket.getInputStream();
PrintWriter out = new PrintWriter(os, true);
BufferedReader in = new BufferedReader(new InputStreamReader(is));

String line = in.readLine(); //WHEN I HAVE THIS CODE ON THE CLIENT
SIDE
if (line.compareTo("OK")!=0) //WITH RESPECT TO WHAT I HAVE ON THE
SERVER SIDE
return; //THE FILE BEING RECEIVED IS MESSED UP, OTHERWISE IS PERFECTLY
RECEIVED

RcvFile(is);

out.close();
in.close();
socket.close();
} catch (IOException e) { }

}

static void RcvFile(InputStream is)
{
try
{
BufferedInputStream ins = new BufferedInputStream(is);
BufferedOutputStream outs = new BufferedOutputStream(new
FileOutputStream(FILE_NAME));
byte[] buffer = new byte[BUFFER_SIZE];

while(true)
{
int count = ins.read(buffer, 0, BUFFER_SIZE);
if (count <= 0)
{
break;
}
outs.write(buffer, 0, count);
System.out.println(buffer);
}
outs.flush();
ins.close();
outs.close();
}
catch(IOException e) {}
}

}


Please let me know what i am doing wrong. From all the playing around
i have done it seems that once i bind and send anything via the
printwriter the Bufferedinputstream doesn't work properly.

Thanks a million

JatP

Jul 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jochen Daum | last post by:
Hi, I have to emulate a "file upload" to a Java Servlet which is done with the class URLConnection. The java source basically does URLConnection conn =...
9
by: | last post by:
Java has packages (libraries) for everything. EVERYTHING. Instead, C++ is too poor in its classes. Is there a concept to extend C++ with other standard libraries, about multithreading, socket...
3
by: Jay | last post by:
Hi, I implemeneted an FTP client and server long time back using Java. I found sockets porgramming in java quite useful and easy to handle. I now wanted to implement a similar program using C++....
31
by: somebody | last post by:
No, I'm not a troll, just angry. I just started learning C++, and find it extremely lacking compared to Java. For example, I had to write my own functions to do something as simple as a case...
23
by: vinod.bhavnani | last post by:
Hello all, I need desperate help Here is the problem: My problem today is with multidimensional arrays. Lets say i have an array A this is a 4 dimensional static array.
4
by: jediknight | last post by:
Hi, I am trying to establish whether I can have a C# socket server and Java socket clients. From all the examples I have seen on the web refer to Java Servers and C# clients. Can anyone point...
3
by: Dameon99 | last post by:
Hi.. Im experiencing a weird error I dont know how to fix. I have a scanner object and whenever I use nextInt, anything above 3 causes the program to crash saying and links me to this line of the...
0
by: yjh0914 | last post by:
hi guys! so im basically editting my post i made earlier as it wznt as specific.. i have to make a program that basically ranks students by their cumulative gpa. the student's info is on a csv file...
1
by: brendanmcdonagh | last post by:
Hi all, I have my little message program working until i press a button and then it'll go to actionPerformed method, and stop reading but will keep sending a message which is being received....
4
by: kingmathyou | last post by:
I have been trying to figure it out this program for the passed two hours with no luck. It is probably very easy, but I can't figure it out. Here is what I have to do: Write a program that asks...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.