Can I re-use a socket  (2 way connection)

I am sending various types of files across an socket. The files are going accross just fine. I do close the output stream on the client which wraps the socket. However, I would like to keep the socket open and send an XML file back to the client containing some information about what finally happens to the file. However, if I don't close the socket the file remains open and then will get munged. So, I guess I need a way to close the file writer but keep the socket open. Anyone know if this is possible or how to do this?
Here's my client/server.
<client>
...// basically gets the file name.
Socket sock = new Socket( host, port );
File fp = new File(fileName);
FileInputStream fins = new FileInputStream(fp);
long fsize = fp.length();
int bsize = 1024000;
byte[] bdata = new byte[bsize];
DataOutputStream o = new DataOutputStream(sock.getOutputStream());
int n=0;
while (fsize>0)
n = fins.read(bdata,0,bsize); // read 1mb from file
o.write(bdata,0,n); // write it to the socket
fsize-=n;
o.close();
System.out.println("File Sent");
</client>
<server>
public class XMLServer implements Runnable {
public static int DEFAULT_PORT = 22334;
private Socket request = null;
public XMLServer(Socket request) {
this.request = request;
public XMLServer(int port) {
try {
ServerSocket server = new ServerSocket( DEFAULT_PORT );
System.out.println( "Server starting" );
while ( true )
// Wait for client request
System.out.println( "Waiting...\n" );
Socket socket = server.accept();
System.out.println( "Request from client received. Processing request." );
// Handle request
XMLServer handler = new XMLServer( socket );
Thread thread = new Thread( handler );
thread.start();
} catch(IOException ioex) {
ioex.printStackTrace();
public void run() {
try {
handleRequest();
} catch(IOException ioex) {
System.out.println("Error occurred while trying to service request.");
System.out.println("Server stopped.");
ioex.printStackTrace();
System.exit(0);
public void handleRequest() throws IOException {
File fp = new File(fileName);
FileOutputStream fout = new FileOutputStream(fp);
int bsize = 1024000;
byte[] bdata = new byte[bsize];
DataInputStream i = new DataInputStream(request.getInputStream());
int n=0;
long totalbytes = 0;
System.out.println("Receiving file");
while ((n = i.read(bdata,0,bsize)) > 0)
fout.write(bdata,0,n); // write it to the socket's stream
totalbytes += n;
System.out.println("File Read Complete " + totalbytes + " in length.");
fout.close();
System.out.println("File Closed");
<server>

You understand your problem completely.
While convienient, it is not practical to use the
socket streams that way in your application, because
Sun early on made the decision that closing a stream
wrapper always closes the inner stream. (Personally I
wish they hadn't) Anyway, you'll now have to read the
file into a byte array first, and send that array over
the output stream.
Makes sense?Ahhh.. Ok, so you're saying in order to keep the stream open, I would have to send the file in a packet structure so I can send the server the length of the file (in bytes) and then the file so that the server would know to read N bytes and I could then avoid immediately closing the socket? I guess that sounds ok. Kind o f wierd though.. Maybe I'll just use the ProgressMonitor and let that suffice.
Eeek, tell me it isn't so...

Similar Messages

  • Can Shuffle be used on a computer not connected to the Internet

    Can Shuffle be used on a computer not connected to the Internet. I have access to an Internet connected computer to register but I would like to manipulate and recharge on a machine that is not connected. Thanks for any help you can provide.

    As long as you aren't trying to update/restore it the Shuffle will work on a computer with no Internet access.

  • Can my Macbook use an all-in-one connected to a Windows Home Server

    Hey All, How do I connect my Canon Pixma MX700 to the Windows Home Server and be able to both Print and Scan from my Macbook over the network.

    I just saw this post--it's for a different Canon than yours but it might point you in the right direction:
    "The Canon MF6500 series uses the Canon UFR2 printer driver, which also supports the larger Canon multifunction copiers. If the printer is connected directly to the network via Ethernet, you use the More Printers > Canon IP (UFR2) option to enter the ip address of the printer. If the device is connected via USB, the Default browser view is used. In both cases, the user has no option to manually select the model in the print using menu. The driver communicates with the printer to determine which model it is.
    If you have the printer connected via a Windows share, or to the USB port of a wireless router, the driver cannot 'see' the printer and will not complete the creation of the printer queue.
    For a wireless router with Ethernet ports, you can connect the printer via Ethernet and then use the More Printers function. Of course the printer would require an Ethernet port for this to be possible, and I am not sure if you have such an option on the MF6500. If not, then you won't be able to use the printer the way you want.
    FYI - the Apple Canon driver package is for the inkjet printers only.
    PaHu"

  • Can this be used with out a internet connection?

    Can this be used without WiFi? Like be used as an external hard drive?

    You cannot plug in to the USB as many people would like to do. It can only be used as network drive.
    If the TC is not going to be part of the network.. just in bridge to the main router is fine.. then it is very hard to set two networks. It is possible but as g_wolfman has stated, it is far from great in that role.. USB drives are cheap as, faster than TC and more reliable with TM.

  • I can't browse using my blackberry browser (wifi connection w/ BB curve 8520)

    good day!
    i purchased a blackberry curve 8520 (w/contract) from one of the telecommunication companies in my country - philippines. everything started all good until one day i can no longer open/use my pre-installed blackberry browser.
    everytime i click on the bb browser logo, the screen just does nothing... nothing loads up on my phone.
    then i tried opening the bb browser throgh the task manager panel a message appears stating: "your device does not currently have any browser configuration service book entries. please contact yor service provider to enable the browser on your device" 
    i have tried rebooting the phone several times, both soft and hard.
    can you give me any solution for my problem? TIA.
    P.S. i have unlocked my phone so i can use it on other carriers. but the problem started a month before i did so. my two friends also have the same phone (bought the same day as mine) and they are not experiencing the same problem. 

    seaill09 wrote:
    excuse me ? i got the same problem to i click the REGISTER NOW in the menu and it already sent a message in HOST ROUTING TABLE, but nothing happend ? pls. help
    Hi and Welcome to the Community!
    Here are updated instructions, including notations of the required response from each step. If the response arrives correctly, you move onto the next step. If the response does not arrive correctly, you drop out of the process and contact your mobile service provider for formal support.
    I suggest the following steps, in order, even if they seem redundant to what you have already tried (steps 1 and 2 each should result in a message coming to your BB...please wait for that before proceeding to the next step):
    1) Register HRT
    KB00510 How to register a BlackBerry smartphone with the wireless network
    Please wait for one "registration" message to arrive to your Messages app
    2) Delete and Resend Service Books (pre-BB10 devices only)
    KB05000Delete the service book for the BlackBerry Internet Service email account from the BlackBerry smartphone
    If you have no CMIME entry, then skip the deletion
    KB02830 Send the service books for the BlackBerry Internet Service
    Please wait for "Activation" Messages, one per already configured email account, to arrive in your Messages. If you have no already configured email accounts, please wait 1 hour.
    3) Reboot
    Pre-BB10 Devices ONLY. With power ON, remove the back cover and pull out the battery. Wait about a minute then replace the battery and cover. Power up and wait patiently through the long reboot -- ~5 minutes.
    BB10 Devices. Hold the top button down until the counter reaches zero. Wait for the device to be fully shut down (e.g., nothing at all displayed on the screen, no LED lights, etc.). Hold the top button until the red LED is lit. Wait through the full boot-up process. IF this fails, you can attempt the battery-pull method above, but it is normally NOT recommended unless nothing else works.
    See if things have returned to good operation. Like all computing devices, BB's suffer from memory leaks and such...with a hard reboot being the best cure.
    Hopefully that will get things going again for you! If not, then you should try deleting and re-adding your configurations for the affected email accounts. Otherwise, you should contact your mobile service provider for formal support.
    Good luck!
    Occam's Razor nearly always applies when troubleshooting technology issues!
    If anyone has been helpful to you, please show your appreciation by clicking the button inside of their post. Please click here and read, along with the threads to which it links, for helpful information to guide you as you proceed. I always recommend that you treat your BlackBerry like any other computing device, including using a regular backup schedule...click here for an article with instructions.
    Join our BBM Channels
    BSCF General Channel
    PIN: C0001B7B4   Display/Scan Bar Code
    Knowledge Base Updates
    PIN: C0005A9AA   Display/Scan Bar Code

  • Can the Ipad use yor iphone's internet connection?

    I'm thinking about purchasing an ipad. I already have an iphone. Can the iphone's internet connection be used for the ipad?

    http://support.apple.com/kb/HT4517
    Yes if your iPhone supports it.

  • Hi. Since updating to iOS 6, my iPhone 4S can't connect to Wi-Fi and completely breaks down my broadband connection so I can't even use Internet on all my connected devices, including my computer which had an Ethernet. Help?

    I'm getting incredibly frustrated at Apple because of this, it's causing problems as the Wi-Fi connection will occasionally reconnect and then skip out for whole days when I need it the most. I've researched fixes across the Discussions board and have tried everything from resetting and rebooting my router, resetting my network settings on my iPhone, selecting Auto in the HTTP proxy, selecting auto join, switching off cellular data...practically everything I can do. What are the next steps and when will Apple release and update to resolve this issue?

    I'm getting incredibly frustrated at Apple because of this, it's causing problems as the Wi-Fi connection will occasionally reconnect and then skip out for whole days when I need it the most. I've researched fixes across the Discussions board and have tried everything from resetting and rebooting my router, resetting my network settings on my iPhone, selecting Auto in the HTTP proxy, selecting auto join, switching off cellular data...practically everything I can do. What are the next steps and when will Apple release and update to resolve this issue?

  • Can CS4 be used on a computer not connected to the internet?

    Does anyone know if CS4 be activated on a computer which is not connected to the internet?
    Thank you,
    ~Shawn

    Yes, but you'll need to activate by phone. You'll also need to download all the patches with a different machine and manually apply them.
    Bob

  • Use my Airport Express to connect my PC to my non-aiport wireless network??

    I have a wireless network at my house. Can I somehow use my airport express to connect a pc that does not have a wireless card to my existing (non-airport) wireless network?

    Duplicate
    http://discussions.apple.com/thread.jspa?threadID=624119&tstart=0

  • HT2589 We have purchase 5 apple minis and would like them all on one account.  That way we can monitor the use of these units.  they will be used strickly for a business application.  can I use one account in itunes or must i have multiple.

    We have purchased 5 apple minis and would like them all on one account, that way we can monitor the use of these units.  They will be used strickly for a business application.  Can I use one account in itunes or must i have multiple.

    Not going to happen the way you want it to.
    When you add a gift card balance to the Apple ID, it's available for the Apple ID.
    Probably best to create unique Apple ID's for each... this will also make things easier in the future as purchases are eternally tied to the Apple ID they were purchased with.

  • When you are pulling music from a hard drive and it says music can not be found, is there a way to stop it from doing that every time I play music from my computer? It used to find all music now it only finds 1 song at a time

    When pulling music from a harddrive and it say music can not be found, is there a way to stop this from happening every time I play music on my computer? It used to find all music when serching for song but now it only finds 1 at a time. I have over 2000 songs so I can't go through this every time.

    1) However I do not want to move my itunes Music onto the flash drive, only my Movies, and want to keep my music on my hard drive.
    Is this possible?
    Yes
    2) Do the MacBook Air's not have a separate program other than itunes that can play movies? If so, is there a program I can get to solve this problem? I went onto the App Store to see if there was something I could download, but all the apps were for creating DVD's, not simply playing them.
    If you can play the movies in iTunes, you should be able to open the movie files using Quicktime, which should already be on your system.  If you have already transferred the files to your flash drive, open the flash drive in Finder, and hold control when you click on the movie file (or right click if you have the enabled) and select Open With Quicktime Player.  This should play the movie directly from the flash drive. 
    If you want to set all .m4v files to open with Quicktime by just double clicking on the file, you can follow the steps here: http://www.tech-recipes.com/rx/2573/os_x_how_to_change_default_application_to_op en_file_type/
    3) If 1. & 2. questions are solvable, will it then be safe to delete the movies from my itunes?
    You can move the TV shows to your flash drive by dragging them from the iTunes window to your flash drive in Finder or the flash drive icon on the desktop.  When you are sure everything is transferred over to your external device, you can delete the movie from iTunes.  I recommend playing the videos from the flash drive first before deleting them in iTunes. 
    To delete the videos from iTunes, you can highlight them and press the delete key on your keyboard.  In the next prompt, you would want to move it to the trash so that it is deleted from your internal hard drive.  
    I hope that helps!

  • Is there a way to set my settings in the latest version of Firefox so I can still zoom using the trackpad?

    I have a MacBook. In previous versions of Firefox I could zoom in or out using my trackpad. Is there a way to set my settings in the latest version of Firefox so I can still zoom using the trackpad?

    Some gestures have been removed in Firefox 4.
    You can restore the zoom feature by changing the values of the related prefs on the <b>about:config</b> page.
    browser.gesture.pinch.in -> <b>cmd_fullZoomReduce</b>
    browser.gesture.pinch.in.shift -> <b>cmd_fullZoomReset</b>
    browser.gesture.pinch.out -> <b>cmd_fullZoomEnlarge</b>
    browser.gesture.pinch.out.shift -> <b>cmd_fullZoomReset</b>
    browser.gesture.pinch.latched -> <b>false</b>
    To open the <i>about:config</i> page, type <b>about:config</b> in the location (address) bar and press the "<i>Enter</i>" key, just like you type the url of a website to open a website.<br />
    If you see a warning then you can confirm that you want to access that page.<br />

  • Trying to save files on "PC" side of Mac, using a FreeAgent external harddrive.  I get the error message E:/not accessible or something similar.  Can I not use an external harddrive, if not, what is the best way to back up PC side of a Mac

    Trying to save files on "PC" side of Mac, using a FreeAgent external harddrive.  I get the error message E:/not accessible  access denied.  Can I not use an external harddrive, if not, what is the best way to back up PC side of a Mac

    Apple has really crappy NTFS read-only that does not always work. And it is probably a licensing issue.
    Paragon Software is constantly being updated and supported with their NTFS for Mac OS X - they also have an HFS+ driver for Windows that works - Apple's HFS+ read-only driver for Boot Camp / Windows does not.
    I keep seeing problems and lack of support for MacFUSE, that could change.
    http://www.bing.com/search?q=ntfs+for+mac

  • My mac book is not working so i can no longer use my mac to back up my note in my phone and unfortunately i dont have outlook on my pc is there a way to get all my old notes from my 3gs to my 4s iphone?

    my mac book is not working so i can no longer use my mac to back up my note in my phone and unfortunately i dont have outlook on my pc is there a way to get all my old notes from my 3gs to my 4s iphone?

    Reset, hold both home and power buttons until the iPhone begins to start, try this a few times if necessary. If still problem, you may have to just Restore your iPhone to get it working. In the future sync your iPhone with iTunes routinely and when it contains important data, iCloud when set correctly will do this for you continuously.

  • I can no longer use the keyboard shortcuts, 'command  C' and 'command   V'.  Did I inadvertently do something to my keyboard or computer that caused this?  Is there a way to fix it?

    I can no longer use the keyboard shortcuts, 'command  C' and 'command   V'.  Did I inadvertently do something to my keyboard or computer that caused this?  Is there a way to fix it?

    You can also check in System Preferences > Keyboard > Keyboard Shortcuts, make sure that custom shortcuts were not created using those same commands. If ones were, they could be creating a conflict. Step throuogh each of the items in the left list, checking the array presented in the right pane for each.

Maybe you are looking for

  • Help please! CC Desktop fails to install -- Adobe cleaner has errors...

    Migod, this CC business is more trouble than it is worth. My latest problem is that I cannot update CC Desktop. I followed the steps outlined in another post, to wit: I downloaded and ran the cleaner tool. It completed, but with errors. I tried to re

  • How to call a batch script from DI?

    Hi All, I'm new to DI/DS and would like to know is there a way to call a batch sciprt (.bat file) in DI? What I try to do is, 1.call a batch script to move source files from a file server to the source folder before kicking off the data flow 2. once

  • Problem with customization of graph

    Hi experts, I am facing some problem in customizing graphs created through Report Builder. I am having some values on x-axis like employee name (emp-code). e.g. Janak Nirmal (007) I did one thing that i m only displaying emp-code on x-axis. i.e. 007

  • Frames with link to Struts do not render before mouse over in IE 7.0?

    Hi We have troubles with frames with link to Struts, which do not render before mouse over in IE 7.0? The application works fine with Firefox 2.0 and Internet Explorer 6.0, we have analyzed the cause to be whenever you refer to a struts 1.0 action in

  • Default time period

    It appears that the default "time period" whenever a new event is made is 1 hour. I would prefer 15 or 30 minutes, I can change to this manually , however I wish to make it my default. Any help please. thank you, John