A simple networking program, I can't figure out what's gone wrong

hi, dear all, I programmed a simple server and client, I try to type some in client and see it on the server side, but the server doesn't show the input from client. Please help me out, thanks a lot.
there are two classes, server and client.
server class:
import java.net.*;
import java.io.*;
public class test3 {
/** Creates a new instance of test3 */
public test3() {
public static void main(String[] args) {
try {
ServerSocket server = new ServerSocket(5555);
while (true) {
Socket connection = server.accept( );
try{
// OutputStreamWriter out
// = new OutputStreamWriter(connection.getOutputStream( ));
// out.write("You've connected to this server.\r\n");
// out.flush();
InputStream in = connection.getInputStream( );
StringBuffer input = new StringBuffer( );
int c;
// connection.close( );
int i = 0;
while(i < 10){
while ((c = in.read( )) != -1) input.append((char) c);
String inString = input.toString().trim( );
System.out.println(inString);
i++;
catch (IOException e) {
// This tends to be a transitory error for this one connection;
// e.g. the client broke the connection early. Consequently,
// we don't want to break the loop or print an error message.
// However, you might choose to log this exception in an error log.
/* finally {
// Most servers will want to guarantee that sockets are closed
// when complete.
try {
if (connection != null) connection.close( );
catch (IOException e) {}
catch (IOException e) {
System.err.println(e);
client class:
import java.net.*;
import java.io.*;
public class test4{
public static void main(String[] args){
try{
Socket theSocket = new Socket("192.168.1.102", 5555);
OutputStream theOutputStream = theSocket.getOutputStream();
OutputStreamWriter out = new OutputStreamWriter(theOutputStream);
BufferedReader userIn = new BufferedReader(new InputStreamReader(System.in));
while (true) {
String theLine = userIn.readLine( );
if (theLine.equals(".")) break;
System.out.println(theLine);
out.write(theLine + "\r\n");
out.flush( );
// System.out.println(networkIn.readLine( ));
}// end of while
}// end of try
catch (IOException e) {
System.err.println(e);
}// end of catch
} // end of main
} // end of test4
Thanks again

Well, i don't get the error to, but I use the DataOutputStream and DataInputStream or ObjectOutputStream and ObjectInputStream with the I/O connection.
the basic steps at the server are:
1) create new ServerSocket
2) Waiting for a connection
3) Create a I/O Stream
4) Process the job
5) close the connection
at the client are:
1) connect to the server
2) Create a I/o stream
3) process the job
4) close the connection
Maybe you can send what the specify error you get in your code

Similar Messages

  • I can't figure out what im doing wrong with my File I/O program lol

    I'm new to the java language and was wondering if anybody could help me with the program i an writing. I don't quite understand file I/O but I've given it my best and I'm stuck. I'm supposed to write a program that will read in a file of student academic credit data and create a list of students on academic warning. The list of students on warning will be written to a file. Each line of the input file will contain the
    student name (a single String with no spaces), the number of semester hours earned (an integer), the total quality points earned (a double). The program should compute the GPA (grade point or quality point average) for each student (the total quality points divided by the number of semester hours) then write the student information to the output file if that student should be put on academic warning. A student will be on warning if he/she has a GPA less than 1.5 for students with fewer than 30 semester hours credit, 1.75 for students with fewer than 60 semester hours credit, and 2.0 for all other students. The instructions are :
    1. Set up a Scanner object scan from the input file and a PrintWriter outFile to the output file inside the try
    clause (see the comments in the program). Note that you’ll have to create the PrintWriter from a FileWriter,
    but you can still do it in a single statement.
    2. Inside the while loop add code to read the input file—get the name, the number of credit hours, and the
    number of quality points. Compute the GPA, determine if the student is on academic warning, and if so
    write the name, credit hours, and GPA (separated by spaces) to the output file.
    3. After the loop close the PrintWriter and Scanner objects in a finally block.
    4. Think about the exceptions that could be thrown by this program:
    • A FileNotFoundException if the input file does not exist
    • A InputMismatchException if it can’t read an int or double when it tries to – this indicates an error in the
    input file format
    • An IOException if something else goes wrong with the input or output stream
    Add a catch for each of these situations, and in each case give as specific a message as you can. The
    program will terminate if any of these exceptions is thrown, but at least you can supply the user with useful
    information.
    5. Test the program. Test data is in the file students.txt. Be sure to test each of the exceptions as well.
    My source code is:
    // Warning.java
    // Reads student data from a text file and writes data to another text file.
    import java.util.*;
    import java.io.*;
    public class Warning
    // Reads student data (name, semester hours, quality points) from a
    // text file, computes the GPA, then writes data to another file
    // if the student is placed on academic warning.
    public static void main (String[] args)
         int creditHrs; // number of semester hours earned
         double qualityPts; // number of quality points earned
         double gpa; // grade point (quality point) average
         Scanner scan=null;
         PrintWriter outFile=null;
         String name, inputName = "students.txt";
         String outputName = "warning.txt";
         try
              // Set up Scanner to input file
              scan=new Scanner(new FileInputStream(inputName));
              // Set up the output file stream
              outFile = new PrintWriter(new FileWriter(outputName));
              // Print a header to the output file
              outFile.println ();
              outFile.println ("Students on Academic Warning");
              outFile.println ();
              // Process the input file, one token at a time
              while (scan.hasNext())
                   // Get the credit hours and quality points and
                   // determine if the student is on warning. If so,
                   // write the student data to the output file.
                   name=scan.next();
                   creditHrs=scan.nextInt();
                   qualityPts=scan.nextDouble();
                   gpa=qualityPts/creditHrs;
                   if ((gpa < 1.5 && creditHrs < 30) || (gpa < 1.75 && creditHrs < 60) || (gpa < 2.0 && creditHrs >= 60))
                        outFile.print(name + " ");
                        outFile.print(creditHrs + " ");
                        outFile.print(qualityPts + " ");
                        outFile.print(gpa);
              //Add a catch for each of the specified exceptions, and in each case
              //give as specific a message as you can
    catch (FileNotFoundException e)
    System.out.println("The file " + inputName + " was not found.");
    catch (IOException e)
    System.out.println("The I/O operation failed and " + outputName + " could not be created.");
    catch (InputMismatchException e)
    System.out.println("The input information was not of the right type.");
              //Close both files in a finally block
    finally
         scan.close();
         outFile.close();
    The txt file is:
    Smith 27 83.7
    Jones 21 28.35
    Walker 96 182.4
    Doe 60 150
    Wood 100 400
    Street 33 57.4
    Taylor 83 190
    Davis 110 198
    Smart 75 292.5
    Bird 84 168
    Summers 52 83.2
    The program will run and then terminate without creating warning.txt. How can I get it to create the warning .txt file? Any help that you could give would be greatly appreciated.

    Alright, here is my code reposted:
    // Warning.java
    // Reads student data from a text file and writes data to another text file.
    import java.util.;
    import java.io.;
    public class Warning
    // // Reads student data (name, semester hours, quality points) from a
    // text file, computes the GPA, then writes data to another file
    // if the student is placed on academic warning.
    public static void main (String[] args)
    int creditHrs; // number of semester hours earned
    double qualityPts; // number of quality points earned
    double gpa; // grade point (quality point) average
    Scanner scan=null;
    PrintWriter outFile=null;
    String name, inputName = "students.txt";
    String outputName = "warning.txt";
    try
    // Set up Scanner to input file
    scan=new Scanner(new FileInputStream(inputName));
    // Set up the output file stream
    outFile = new PrintWriter(new FileWriter(outputName));
    // Print a header to the output file
    outFile.println ();
    outFile.println ("Students on Academic Warning");
    outFile.println ();
    // Process the input file, one token at a time
    while (scan.hasNext())
    // Get the credit hours and quality points and
    // determine if the student is on warning. If so,
    // write the student data to the output file.
    name=scan.next();
    creditHrs=scan.nextInt();
    qualityPts=scan.nextDouble();
    gpa=qualityPts/creditHrs;
    if ((gpa < 1.5 && creditHrs < 30) || (gpa < 1.75 && creditHrs < 60) || (gpa < 2.0 && creditHrs >= 60))
    outFile.print(name " ");
    outFile.print(creditHrs " ");
    outFile.print(qualityPts " ");
    outFile.print(gpa);
    //Add a catch for each of the specified exceptions, and in each case
    //give as specific a message as you can
    catch (FileNotFoundException e)
    System.out.println("The file " inputName " was not found.");
    catch (IOException e)
    System.out.println("The I/O operation failed and " outputName + " could not be created.");
    catch (InputMismatchException e)
    System.out.println("The input information was not of the right type.");

  • Background program running and can't figure out what it is...

    I have an IMAC desktop, I just purchased the Photoshop Elements 10 software for MAC, installed it, running fine. But now I have program that is contanstly running in the background that is slowing up all of my other applications I run while I'm working. I've checked the Activity Monitor but I can't see anything out of the norm...Does anyone have any suggestions? I'm very frustrated....

    I presume you mean that the hard drive is working on something. Again, any such process would show in the Activity Monitor. Check that again, confirming that you have "all processes" set so you can see everthing that's going on. It will be easiest to spot anything unusual if you click on the "% CPU" column header to sort by the amount of CPU time each process is using. Click twice so that the arrow is pointing downwards; then the top users of CPU time will be listed at the top. Post the name of any process that's consistently or unexpectedly using a significant amount of CPU time and we can probably figure out what's happening and offer suggestions.
    Regards.

  • My iTunes is no longer working.  I uninstalled and reinstalled but the program won't open and run.  I was trying to download an mp3 file last evening and I think something has been deleted that I need to open the iTunes.  How do i figure out what is gone

    I uninstalled and reinstalled but the program still won't open.  I was trying to download an mp3 file last evening.  Instead the site tried to install a new audio player.  I did not want it so uninstalled it immediately.  I think in that process something was deleted that I need for iTunes but I do not know what.  How do i figure out what is gone and recover it?
    I did try a system restore - but that was not a fix either.

    I was trying to download an mp3 file last evening.  Instead the site tried to install a new audio player.
    Ackk ... that behavior is consistent with the play_mp3.exe trojan:
    http://en.wikipedia.org/wiki/Play_mp3.exe
    Try downloading and installing the free version of Malwarebytes AntiMalware. Update your MBAM definitions and then run a full scan of the PC. (Takes about 2 hour on my Lenovo.)
    http://www.malwarebytes.org/mbam.php
    Does the scan find any infections? If so, please paste the contents of the log file for the scan in a reply here so we can have a look.

  • I uninstalled Firefox once, reinstalled it and it ran. I had a problem so I did it again. Now it will not run and I get an error message saying that firefox is running and you can only run one at a time. I can't figure out what is running.

    Because of a problem, I uninstalled Firefox once, reinstalled it and it ran. I had a problem so I uninstalled/reinstalled it again. Now it will not run. I get an error message saying that firefox is running and you can only run one at a time. I have uninstalled multiple times and can't figure out what is running. The is only one Firefox installed and it is not open. What does this mean and how do I fix it?

    If you use ZoneAlarm Extreme Security then try to disable Virtualization.
    *http://kb.mozillazine.org/Browser_will_not_start_up#XULRunner_error_after_an_update
    See also:
    *[[/questions/880050]]

  • Am getting message from MacPro that my start up disc is full - but I can't find it and can't figure out what to do to help situation. I've been making a number of imovies, which generates junk files. help?

    I am getting message from MacPro that my start up disc is full - but I can't find this "start up disc" and can't figure out what to do to help situation. I've been making a number of imovies, which generates junk files and material that I should toss in the trash, but it is not clear to me  what items I can toss and which items I can't toss. Can you help? Using the imovie "help" support the system showed me under the menu item "go" where the "start up disc" should be - but that wasn't actually available on my menu!  Thanks for your help!

    Disk Utility 
    Get Info on the icon on Desktop
    Try to move this to the MacBook Pro forum
    Your boot drive should be 30% free to really perform properly. 10% minimum
    Backup, clone, use TimeMachine, use another drive for your projects and movies, replace and upgrade the internal drive even.

  • Can't figure out what I'm doing wrong

    I'm using Studio 8 in Mac OS X (10.4.6). I've got some pdfs
    uploaded to my site that are accessible through links one one of
    the pages. I had to make changes to the pdfs and resave them. I
    thought I uploaded them, but when I test the links it downloads the
    old version and I can't figure out what I'm doing wrong. Here's
    what I did.
    This may be a dumb way to do things, but I saved the new
    version and then dragged into the server file in
    user-->Library-->Application
    Support-->Macromedia-->Dreamweaver
    8-->Configuration-->ServerConnections-->unnamed
    server-->public_html (this is where all the other files are).
    When I go back into Dreamweaver and open the pdf listed in the
    Files window on the right (by control-clicking and selecting Open
    with-->Acrobat) the correct version appears. I thought maybe
    there was a delay or something, but I've waited and still the old
    version is what you get from the site.
    Is there an easier way to do this, or is something wrong
    maybe? Many thanks in advance.

    > This may be a dumb way to do things, but I saved the new
    version and then
    > dragged into the server file in
    user-->Library-->Application
    > Support-->Macromedia-->Dreamweaver
    Put the files in this Site's Local Site Folder.
    If unsure of where you've specified this to be, look in this
    site definition
    (dw menu-->Sites-->Manage or Edit Sites)
    If that path you've given above above is correct, it's
    wrong....

  • Trouble with calculating fields. Can't select (check) fields. Also can't figure out what's wrong with a division field (percent) that I created. Keep getting the pop up that format of the field doesn't allow blah blah blah... Help!

    Trouble with calculating fields. Can't select (check) fields. Also can't figure out what's wrong with a division field (percent) that I created. Keep getting the pop up that format of the field doesn't allow blah blah blah... Help!

    1. Use the mouse to select the field and then press the space bar.
    2. A null string is the same as zero. What is the result for division by zero?

  • When I put my iphone 4s into my pocket (pants or coat) and move it around, the phone beeps/makes a tone. I can't figure out what is causing the noise.

    I have a strange problem. When I put my iphone 4S into my pocket (pants or coat) and move it around, the phone makes a beep noise/tone. I can't figure out what is causing the noise. It doesn't sound like any of my regular applications. Maybe it is the lock noise but I can't tell for sure.  More movement makes more beeps.  Even walking with my phone in my pocket creates beeps.  The IOS is 7.0.4 (11B554a).  Anyone else have this crazy problem?

    Try This...
    Close All Open Apps... Sign Out of your Account... Perform a Reset...
    Reset  ( No Data will be Lost )
    Press and hold the Sleep/Wake button and the Home button at the same time for at least ten seconds, until the Apple logo appears. Release the Buttons.
    http://support.apple.com/kb/ht1430
    If the issue persists...
    Connect to iTunes on the computer you usually Sync with and Restore
    http://support.apple.com/kb/HT1414
    Make sure you have the Latest Version of iTunes (v11) Installed on your computer
    iTunes free download from www.itunes.com/download

  • Local hostname already in use - can't figure out what's going on

    I've been having issues with a wireless router and I think the problem lies somewhere within my computer. As I switch back and forth from trying to connect to the internet via my router or directly connected to my cable modem, I will often get this error:
    "This computer's local hostname "xyz" is already in use on this network"
    There is only one compuer on the network, however and I cannot figure out what's going on.
    I have 2 users set up on this machine, but I'm the admin and acting as such. Even with no other networked equipment hooked up to my Mac (except for the cable modem) this problem won't go away.
    I guess my real quesiton is what causes this and where should I go to troubleshoot things?
    I'm running 10.4.6, connect to my ISP using DHCP and have the same problem with both a D-Link and Linksys router.
    Any ideas?
    Thanks!

    Thanks very much for your help, fu.
    If your previous setup follows a flow like this: RCA
    CableModem-->NetGear Router-->AirPortExpress, there's
    something misconfigured routing-wise in your network.
    You have to decide which device (Netgear or AirPort
    Xpress) will handle the routing in your network.
    Previous to these problems, that was the basic flow of my network. The netgear router also fed a wired connection into a Wintel PC, and the AirPort Express hosted a wireless network that was extended by a second Airport Express. And that second AE connected to a ReplayTV with a wired connection. And then, of course, the PowerBook got Internet access wirelessly through the AE-hosted network. And that was it.
    That network always was configured so that the netgear router assigned IP addresses using DHCP, and the Airport Expresses would not assign IP addresses (i.e., "Distribute IP addresses" was NOT checked when you look at the configuration in Airport Admin Utility).
    How big is your network j? just 1 Mac (your
    PowerBook)?
    That was the state of the network a couple of days ago when I posted this. I have established that the PowerBook gets access to the Internet just fine directly though the cable modem, and just fine when it is the only wired connection to the router.
    Let's deal with your Mac first:
    Start with your Mac connected directly to the cable
    Modem. (When your mac is attached directly to the
    Cable Modem, do you use DHCP?)
    Go to SystemPreferences-->Sharing and type a Computer
    Name other than the default one
    Open Applications-->Utilities-->Terminal and type
    lookupd -flushcache (press Return) and reboot
    the Mac.
    I did this.
    One way to fix your network is to have the NetGear do
    the routing, and the AirPort Express to (just) extend
    your (wired) network wirelessly (No routing will be
    done from the AirPortXPress, (No distribution of IP
    Addresses)
    I soft reset both of my Airport Expresses. I configured the first one so that it was hosting a network, but not distributing IP addresses, and the second so that it was extending that wireless network. And I plugged the host AE into the netgear router. And for a little while, that was working.
    But then I got a message saying "IP Configuration 192.168.0.4 in use by 00:04:40:06:1f:82, DHCP Server 192.168.0.1". The dialog box with that message will come up periodically as long as the Airport Expresses are plugged into power and one is plugged into the router. Also, I once agaiin received the message saying the local hostname (the completely original one that I had created) was in use, and the name was being changed.
    Here is the thing. I cannot figure out what has that ...0.4 IP address and ...1f:82 MAC address on the network. That is not the "Internet port MAC address" or the "LAN port MAC address" for the Netgear router. It is also not the MAC address for either of the Airport Expresses or the PowerBook's internal Ethernet card.
    I accessed the router through my browser, and clicked on "Attached Devices." Here is the list of Attached Devices:
    # IP Address Device Name MAC Address
    1 192.168.0.2 00:11:24:08:4c:88
    2 192.168.0.3 00:14:51:6d:15:7c
    3 192.168.0.5 00:0a:95:da:4f:24
    The first two addresses are the two AE's, and the third is the internal Ethernet card of the PowerBook.
    I ran "Netstat" under the Network Utility to get "Routing Table Information," and here is what I get (with some identifying info removed):
    Internet:
    Destination Gateway Flags Refs Use Netif Expire
    default 192.168.0.1 UGSc 23 10 en0
    127 localhost UCS 0 0 lo0
    localhost localhost UH 11 2400 lo0
    169.254 link#4 UCS 0 0 en0
    192.168.0 link#4 UCS 3 0 en0
    192.168.0.4 link#4 UHLW 1 1 en0
    192.168.0.5 localhost UHS 0 14 lo0
    Internet6:
    Destination Gateway Flags Netif Expire
    localhost link#1 UHL lo0
    localhost Uc lo0
    localhost link#1 UHL lo0
    link#4 UC en0
    justin-and-***** 0:a:95:da:4f:24 UHL lo0
    **************bedro 0:14:51:6d:15:7c UHLW en0
    link#5 UC en1
    justin-and-***** 0:a:95:f3:3e:df UHL lo0
    **************bedro 0:14:51:6d:15:7c UHLW en1
    ff01:: localhost U lo0
    ff02::%lo0 localhost UC lo0
    ff02::%en0 link#4 UC en0
    ff02::%en1 link#5 UC en1
    Any ideas? Thanks again for your help.

  • I have a MacBook Pro OS 10.9.5 with 250 GB hard drive-- I can't figure out what is taking up all my hard drive space (it is not backups).  Any ideas?

    MacBook Pro OS 10.9.5, 250 GB solid state hard drive.
    I can't figure out what is taking up all my hard drive space (it is not backups).  Any ideas? 

  • I probably misclicked when I activated the security lock on the iPhone and now I can not figure out what password I entered. Moreover, I have patches and funky looking phone so I can not restore iphine pres itunes. Please advice.

    I probably misclicked when I activated the security lock on the iPhone and now I can not figure out what password I entered. Moreover, I have patches and funky looking phone so I can not restore iphine pres itunes. Please advice.

    Your only recourse is to force it into DFU mode:
    Turn your phone off and connect your cable to the computer, but not the device just yet. Start up iTunes. Now, hold down the home button on your phone and plug it in to the cable - don't let go of the button until iTunes tells you it's detected a phone in recovery mode. Now you can restore to factory settings.

  • I see "imac-54f9d5" under the "Shared" heading in my finder. I am not connected to any printers or other Mac devices. Is it possible that someone is remotely accessing my system? How can I figure out what this is?

    I see "imac-54f9d5" under the "Shared" heading in my finder. I am not connected to any printers or other Mac devices. Is it possible that someone is remotely accessing my system? How can I figure out what this is?

    is it your imac?
    is it your your router's name?
    are you by any chance connected to another wifi router then your own because the other one was open and not password protected?

  • TS3694 My iPad says it has to restore to update, but it will not.  It is caught in a cycle of not being able to restore but needed to restore.  I keep getting error code 3014 but I can not figure out what this means!

    My iPad says it has to restore to update, but it will not.  It is caught in a cycle of not being able to restore but needed to restore.  I keep getting error code 3014 but I can not figure out what this means! nor can I do anything about it!  So Frutrated!

    Also...
    See Here  >  http://support.apple.com/kb/HT1808
    You may need to try this More than Once...
    But... if the Device has been Modified... this will Not necessarily work.

  • I recently received a  WD My Book Studio external Hard drive and am installing it but can't figure out what the difference between wd turbo installer  64 and wd   turbo installer is.  Can someone tell me.  Thanks in advance for all your help.

    I recently received a  WD My Book Studio external Hard drive and am installing it but can't figure out what the difference between wd turbo installer  64 and wd   turbo installer is.  Can someone tell me.  Thanks in advance for all your help.

    Oh ok, well that was what the test said prior to reformatting the hard drive. Now, it doesn't find any trouble no matter how many times I run it. That's what has me so confused, everything seems to check out and yet...I can't do anything, not even install the software!

Maybe you are looking for

  • A bad scare: root file system recovery [SOLVED]

    Hi, everybody, The trouble began with an odd message: KDE Daemon: new storage detected (hard disk): open in a new window, ignore. Unfortunately, the hard disk in question is statically mounted via /etc/fstab; should have been mounted all along. # /et

  • Find a text frame on the page with script label

    hello to all I need to create a script to find a text frame on the page with script label "xxx" and read its contents into a variable. The content of the text frame is a number. thanks

  • Need help regarding certification alternative to weblogic portal developnme

    Hi All, I was planning to give weblogic portal certification (1z0-110). But I found that the 1z0-110 certification is retired. Can anybody guide me to some other alternative. I am a JAVA developer who is interested in doing some server certification.

  • Item Category Assignment Open Interface concurrent program

    Hi, A quick question, Can anyone please tell me why "Item Category Assignment Open Interface concurrent program" is used for. Purpose of running the program. Thanks in advance.

  • Siebel Server Configuration failed

    I am able to configure Siebel gateway name server, siebele enterprise server, but when i configure Siebel Server it shows execution failed... Do i need ti install any language pack Siebel Server configuration? May be that is reason why i am not able