Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Try to Converting TCP to UDP

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Wink Try to Converting TCP to UDP

    sorry i'm nob in java programming,
    i'm high schooler who want learn java, but i have no teacher, please guide me , , ,

    Here is the Client from book, then this code called using another script,,, connect -> openFile -> copyFile -> closeFile;
    import java.io.*;
    import java.net.*;
     
    import com.deitel.ch17.AccountRecordSerializable;
     
    public class ClientRemCopySeqFile{
    	private ObjectInputStream input;
    	private ObjectOutputStream socketOut;
    	private Socket client;
    	String host = "localhost";
     
    	// enable user to select file to open
    	public void openFile()
    	{
    		try // open file
    		{
    			input = new ObjectInputStream(
    			new FileInputStream( "clients.ser" ) );
    		} // end try
    		catch ( IOException ioException )
    		{
    			System.err.println( "Error opening file." );
    		} // end catch
    	} // end method openFile
     
    	public void connect() throws Exception
    	{
    		BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
    		DatagramSocket clientSocket = new DatagramSocket();
    		InetAddress IPAddress = InetAddress.getByName("localhost");
    			byte[] sendData = new byte[1024];
    			byte[] receiveData = new byte[1024];
    		DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
    		clientSocket.send(sendPacket);
    	}
     
    	public void copyFile()
    	{
    		AccountRecordSerializable record;
    		try // input the values from the file
    		{ 
    			while ( true )
    			{
    			record = ( AccountRecordSerializable ) input.readObject();
     
    			// display record contents
    			System.out.printf( "%-10d%-12s%-12s%10.2f\n",
    			record.getAccount(), record.getFirstName(),
    			record.getLastName(), record.getBalance() );
    			socketOut.writeObject( record ); // output record
    			} // end while
    		} // end try
    		catch ( EOFException endOfFileException )
    		{
    			System.out.println("Records has been sent...");
    			return; // end of file was reached
    		} // end catch
    		catch ( ClassNotFoundException cnf )
    		{
    			System.err.println( "Unable to create object."+cnf.toString() );
    		} // end catch
    		catch ( IOException ioException )
    		{
    			System.err.println( "Error during reading from file." );
    		}  // end catch// end catch
     
    	}
     
    	// close file and terminate application
    	public void closeFile()
    	 {
    		try // close file and exit
    		{
    		  if ( input != null )
    			input.close();
    		  if ( client != null )
    			client.close();
    		  if ( socketOut != null )
    			socketOut.close();
    		System.out.println("connection for "+host+" has been terminated ..."); 
    		  System.exit( 0 );
    		} // end try
    		catch ( IOException ioException )
    		{
    		  System.err.println( "Error closing file." );
    		  System.exit( 1 );
    		} // end catch
    	 } // end method closeFile
    }
     
     
    Here the Server from the book too, called using another program, connect -> openFile -> listen -> copyFile -> closeFile 
    import java.io.*;
    import java.net.*;
     
    import com.deitel.ch17.AccountRecordSerializable;
     
    public class ServerRemCopySeqFile{
    	private ServerSocket server = null;
    	private Socket client=null;
    	private ObjectInputStream streamIn;
    	private ObjectOutputStream output;
     
    	public void connect() throws Exception
    	{
    		DatagramSocket serverSocket = new DatagramSocket(9876);
                byte[] receiveData = new byte[1024];
                byte[] sendData = new byte[1024];
                while(true)
                   {
                      DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
                      serverSocket.receive(receivePacket);
                      String sentence = new String( receivePacket.getData());
                      InetAddress IPAddress = receivePacket.getAddress();
                      int port = receivePacket.getPort();
                   }
     
    	 }
     
    	 public void listen(){
    		try {
             client = server.accept();
    		} catch (IOException e) {
             System.out.println("Did not accept connection: " + e);
             System.exit(1);
    		}
     
    		System.out.println("Client connection accepted. Moving to local port ...");
    	 }
     
    	// enable user to select file to open
    	public void openFile(){
    		try // open file
    		{
    		  output = new ObjectOutputStream(
    		  new FileOutputStream( "remclients.ser" ) );
    		} // end try
    		catch ( IOException ioException )
    		{
    		  System.err.println( "Error opening file." );
    		} // end catch
    	} // end method openFile
     
    	public void copyFile(){
    		AccountRecordSerializable record;
    		try // input the values from the file
    		{ 
    			streamIn = new ObjectInputStream(new BufferedInputStream(client.getInputStream()));
    		  while ( true )
    		  {
    			record = ( AccountRecordSerializable ) streamIn.readObject();
     
    			// display record contents
    			System.out.printf( "%-10d%-12s%-12s%10.2f\n",
    			record.getAccount(), record.getFirstName(),
    			record.getLastName(), record.getBalance() );
    			output.writeObject( record ); // output record
     
    		  } // end while
    		} // end try
    		catch ( EOFException endOfFileException )
    		{
    			System.out.println("Records has been save...");
    		  return; // end of file was reached
    		} // end catch
    		catch ( ClassNotFoundException classNotFoundException )
    		{
    		  System.err.println( "Unable to create object." );
    		} // end catch
    		catch ( IOException ioException )
    		{
    		  System.err.println( "Error during reading from file." );
    		}  // end catch// end catch
    	}
     
    	// close file and terminate application
    	public void closeFile()
    	{
    		try // close file and exit
    		{
    			if ( server != null )
    				server.close();
    			if ( client != null )
    				client.close();
    			if ( streamIn != null )
    				streamIn.close();     
    			System.out.println("All connections and file has been closed ..."); 
    			System.exit( 0 );
    		} // end try
    		catch ( IOException ioException )
    		{
    			System.err.println( "Error closing file." );
    			System.exit( 1 );
    		} // end catch
     
    	} // end method closeFile
    }

    Please help me, after run I got error message
    Exception in thread "main" java.lang.NullPointerException
    at ClientRemCopySeqFile.copyFile(ClientRemCopySeqFile .java:51)
    at ClientRemCopySeqFileTest.main(ClientRemCopySeqFile Test.java:13)
    Attached Files Attached Files
    Last edited by Norm; January 9th, 2013 at 11:59 AM. Reason: added code tags


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Try to Converting TCP to UDP

    Exception in thread "main" java.lang.NullPointerException
    at ClientRemCopySeqFile.copyFile(ClientRemCopySeqFile .java:51)
    There was a variable with a null value on line 51 when it was executed. Look at line 51, find the variable with the null value and then backtrack in the code to see why that variable does not have a valid value.
    If you can not tell which variable is null, add a println after line 50 that prints out the values of all the variables used on line 51.


    On future posts Please wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Try to Converting TCP to UDP

    Thanks for reply my question,
    and in the future i'll using ur suggestion when post , , ,

    I have already fix the problem in line 50 and 51, and the program of client show the content of file "clients.ser"
    but there is still a problem, the content of file won't show in the server, do you have solution for it?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Try to Converting TCP to UDP

    do you have solution for it?
    Not without seeing the code.
    Can you post a small, complete program that compiles, executes and shows the problem?

    the content of file won't show in the server,
    Can you explain in more detail what the code does and what you want it to do?

    One problem I see in the code you posted is that the code in the catch blocks do not call the printStackTrace() method to give a full error message.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jan 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Try to Converting TCP to UDP

    oh i'm sorry master,
    the problem of line 50 and 51 is in the AccountRecordSerializable, and the line 50 to record data from "clients.ser",
    and from the book i got, there is nothing AccountRecordSerializable.java, and i think it's over when i can't get the tracking of line 50 in AccountRecordSerializable, because in TCP port line 50 work properly, and i think the problem in AccountRecordSerializable,
    sorry for disturbing you master , , ,

    do you have tutorial to build client-server via udp for transfer file?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Try to Converting TCP to UDP

    Sorry, I don't have any sample code.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jan 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Try to Converting TCP to UDP

    Quote Originally Posted by Norm View Post
    Sorry, I don't have any sample code.
    ok master,
    i'll try to googling again , , ,

Similar Threads

  1. Streaming using UDP
    By frestix in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 28th, 2019, 01:51 PM
  2. UDP Server, I/O problems
    By Mnelson in forum What's Wrong With My Code?
    Replies: 0
    Last Post: July 6th, 2012, 09:13 AM
  3. Handling TCP and UDP in same server
    By ToshX in forum Java Networking
    Replies: 2
    Last Post: December 2nd, 2011, 03:19 PM
  4. [SOLVED] Basics With UDP Packet
    By servalsoft in forum Java Networking
    Replies: 0
    Last Post: October 13th, 2011, 12:47 PM
  5. TCP Over UDP
    By azarakhsh in forum Java Networking
    Replies: 3
    Last Post: July 15th, 2009, 07:21 AM