socket programming - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

socket programming

asd2183asd2183 Posts: 6Member
hello all,
need urgent help. am a new bee for java, have designed code for sending and receiving data using udp socket . kindly check whether my program is correct.

for sending data

package sender;
import java.io.*;

import java.net.*;

public class UDPClient {

public static void main(String[] args) {

String dirname = "/home/shashi/ganesh";
File f1 = new File(dirname);
byte byteRead[] = new byte[10000];
byte buf[]= new byte[50000];
FileInputStream infile = null;
FileOutputStream outfile = null;
DatagramSocket aSocket = null;
int ch;
try{
aSocket = new DatagramSocket();



// byte [] m = args[0].getBytes();
InetAddress aHost = InetAddress.getByName(null);
int serverPort = 6440;


// FileOutputStream outfile = null;

if (f1.isDirectory()){
System.out.println("Directory of " + dirname);
String s[] = f1.list();
for(int i=0; i < s.length;i++){
File f = new File(dirname + "/" + s[i]);
System.out.println("START OF ------- " + s[i]);

try{
infile = new FileInputStream (dirname +"/" + s[i] );

while((ch = infile.read()) != -1){


//for(ch=0;ch<3;ch++){

outfile = new FileOutputStream("/home/shashi/god/x.wav");


byteRead[ch] = (byte) infile.read(buf) ;
System.out.println("sending data");


DatagramPacket request = new DatagramPacket(byteRead,byteRead.length, aHost, serverPort);
aSocket.send(request);
outfile.write(byteRead);
//byteRead[0] = 0;

//
}
System.out.println("END OF --------" + s[i]);
//}
}
catch (FileNotFoundException e)
{
System.out.println("File not found");
}
catch(IOException e)
{
System.out.println("e.getMessage()");
}
{
try
{
infile.close();
//outfile.close();
}
catch (IOException e){}
}if (f.isDirectory()){
System.out.println(s[i] + "is a directory");
} else {
System.out.println(s[i] + "is a file");

}


}
}

else {
System.out.println(dirname + " is not a directory");

}


}
catch (SocketException e) {
System.out.println("Socket: " + e.getMessage());
}
catch (IOException e) {
System.out.println("IO: " + e.getMessage());
}
finally {
if (aSocket != null)
aSocket.close();

}


}
}



for receiving data

import java.net.*;
import java.io.*;
public class UDPServer {

/**
* @param args
*/
public static void main(String[] args) {
DatagramSocket aSocket = null;
//FileInputStream infile = null;
FileOutputStream outfile = null;
//byte []buf= new byte[100];
//byte byteRead;
// if (args.length < 1) {
/// System.out.println("Usage: java UDPServer <Port Number>");
// System.exit(1);
//}
try {
int socket_no = 6440;
aSocket = new DatagramSocket(socket_no);
byte[] buffer = new byte[5000];
while(true) {

try
{

outfile = new FileOutputStream("/home/shashi/god/g.wav");
//for(int i=0;i != -1;i++){
//byteRead = (byte) infile.read() ;
for(int i=0;i<10;i++){
DatagramPacket request = new DatagramPacket(buffer,buffer.length);
// outfile = new FileOutputStream("/home/shashi/god/g.wav");
//outfile.write(buffer);
aSocket.receive(request);

System.out.println("recieved ");

outfile.write(buffer);

}
} //DatagramPacket reply = new DatagramPacket(request.getData(),
// request.getLength(),request.getAddress(),
// request.getPort());
//aSocket.send(reply);
catch (FileNotFoundException e)
{
System.out.println("File not found");
}
catch(IOException e)
{
System.out.println("e.getMessage()");
}
{
try
{

outfile.close();
}
catch (IOException e){}
}



}
}
catch (SocketException e) {

System.out.println("Socket: " + e.getMessage());
}
// catch (IOException e) {
// System.out.println("IO: " + e.getMessage());
//}
finally {
if (aSocket != null)
aSocket.close();
}

}

}


Thanks in advance















Sign In or Register to comment.