1 Attachment(s)
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;
Code :
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)
Re: Try to Converting TCP to UDP
Quote:
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.
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?
Re: Try to Converting TCP to UDP
Quote:
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?
Quote:
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.
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?
Re: Try to Converting TCP to UDP
Sorry, I don't have any sample code.
Re: Try to Converting TCP to UDP
Quote:
Originally Posted by
Norm
Sorry, I don't have any sample code.
ok master,
i'll try to googling again , , ,