Socket Programming

Dear Guys

The problem is that the Media Server sends a packet back to the Client but the client doesn't pick up the send packet at all the client does is wait for the packet.

I spend most of Saturday trying to it out if anyone knows a solution let me thanks

The Media is a server keeping track of clients
Clients is the client


Client.java

//package assignment3_mediaserver;
import java.util.ArrayList;
import java.net.*;
import java.io.*;
import java.util.Arrays;

/**
 * @version 12/05/2014
 * @author Mat
 */
public class Client extends Thread {
    DatagramPacket recivePacket;
    DatagramPacket sendPacket;
    final int DEFAULT_BUFFER = 100;
    final int PORT = 4001;
    byte[] sendData;
    byte[] reciveData;
    DatagramSocket udpSocket;
    String server;
    ArrayList<String> listMP3;
    InetAddress IPaddress;
    BufferedReader keyboard;
    
    public void run() {
        
        try {
          
          while (true) {            
            //Socket tcpServer = welcomeSocket.accept();
            
          }
          
        } catch (Exception e) {
        }   
    }
    
    public void getArgs(String[] args) {
        if(args.length == 1) {
            server = args[0];
        } else {
            System.exit(1);
        }
    }
    
    public void Part_1() {
        try {
            udpSocket = new DatagramSocket();  
            IPaddress = InetAddress.getByName(server); 
            String start = "Im online Server";
            sendData = new byte[DEFAULT_BUFFER];
            sendData = start.getBytes();
            sendPacket = new DatagramPacket(sendData, sendData.length, IPaddress, 4001);
            udpSocket.send(sendPacket); 
        } catch (SocketException e) {
            System.err.println("Cant connect to Port");
            e.getStackTrace();
        } catch (UnknownHostException e) {
            System.err.println("Cant connect to Server");
            e.getStackTrace();
        } catch (IOException e) {
            System.err.println("Cant send Packet 01");
            e.getStackTrace();
        }
    }
    
    public void Part_2() {
        try {
            // Select the folder with .mp3 in it
            File path = new File("C:\\Users\\Matthew\\Documents\\NetBeansProjects\\Assignment3_MediaServer\\src\\music");
            
            listMP3 = new ArrayList<>();
            File[] files = path.listFiles();
            for (File file : files) {
                if (file.isFile()) {
                    //this line weeds out other directories/folders
                    if (file.toString().contains(".mp3")) {
                        listMP3.add(Arrays.toString(files));
                    }
                }
            }
            
            int sizeOfMP3 = listMP3.size();
            sizeOfMP3 = sizeOfMP3 * 512;
            
            String temp = "";
            for(String count : listMP3) {
                count += ", ";
                temp = count;
            }
            sendData = new byte[sizeOfMP3];
            sendData = temp.getBytes();
            sendPacket = new DatagramPacket(sendData, sendData.length, IPaddress, 4001);
            udpSocket.send(sendPacket);
        } catch (IOException e) {
            System.err.println("Cant send Packet 02");
            e.getStackTrace();
        }
    }
    
    public void Part_3() {
        keyboard = new BufferedReader(new InputStreamReader(System.in));
        String input = "";
           
        try {
            System.out.println("Name a song to search for: ");
            input = keyboard.readLine();
            input = input.trim();
        } catch (IOException e) {
            System.err.println("Cant read user input");
            e.getStackTrace();
        }
            
        try {
            sendData = new byte[400];
            sendData = input.getBytes();
            sendPacket = new DatagramPacket(sendData, sendData.length, IPaddress, 4001);
            udpSocket.send(sendPacket);
        } catch (IOException e) {
            System.err.println("Cant send Packet 03");
            e.getStackTrace();
        }
    }
     
    public void Part_4() {
        try {
            reciveData = new byte[1000];
            recivePacket = new DatagramPacket(reciveData, reciveData.length);
            udpSocket.receive(recivePacket);
            String toLine = new String(recivePacket.getData());
            System.err.println(toLine + "Part 4");
        } catch (Exception e) {
            System.err.println("Cant recive Packet");
            e.getStackTrace();
        }
    }
    
    public void Part_5() {
        
    }
    
    public void close() {
        udpSocket.close();
    }
    
    
    /**
     * The method that starts the program
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Client client = new Client();
        client.getArgs(args);
        client.Part_1();
        client.Part_2();
        client.Part_3();
        client.Part_4();
        client.close();
    }
}



Media_Server.java

//package assignment3_mediaserver;

import java.io.*;
import java.net.*;
import java.util.ArrayList;


/**
 * @version 12/05/2014
 * @author Matthew
 */
public class Media_Server {
    DatagramPacket recivePacket;
    DatagramPacket sendPacket;
    final int DEFAULT_BUFFER = 256;
    final int BIG_BUFFER = 2048;
    final int PORT = 4001;
    byte[] sendData;
    byte[] reciveData;
    DatagramSocket udpSocket;
    InetAddress IPaddrss;
    PrintWriter myOutput;
    FileOutputStream file;
    ArrayList<String> ClientMusic;
    String requestSong;
    String IPNo;
    String line;
    
    public void StartUDP() {
        try {
            udpSocket = new DatagramSocket(PORT);
        } catch (SocketException ex) {
            System.err.println("Can't open socket");
            ex.getStackTrace();
        }
    }
    
    
    public void Part_1() {
            try {
                String welcomeLine;
                reciveData = new byte[DEFAULT_BUFFER];
                recivePacket = new DatagramPacket(reciveData, reciveData.length);
                udpSocket.receive(recivePacket);
                welcomeLine = new String(recivePacket.getData());
                welcomeLine = "Im online now";
                System.out.println(welcomeLine);
        } catch (IOException e) {
                System.err.println("Cant get Packet error 01");
                e.getStackTrace();
        }    
    }
    
    public void Part_2() {
        IPaddrss = recivePacket.getAddress();
        System.out.println("Welcome " + IPaddrss.getHostAddress() + "\n");
    }
    
    public void Part_3() {
        try {
            reciveData = new byte[BIG_BUFFER];
            recivePacket = new DatagramPacket(reciveData, reciveData.length);
            udpSocket.receive(recivePacket);
            line = new String(recivePacket.getData());
            line = line.trim();
        } catch (IOException e) {
            System.err.println("Cant get Packet 02");
            e.getStackTrace();
        }
    }
    
    public void Part_4() {
        try {
            line = line.replace("[", "");
            line = line.replace("]", "");
                
            file = new FileOutputStream("songlist.txt", true);
            myOutput = new PrintWriter(file, true); 
            myOutput.println(IPaddrss.toString() + ", " + line + "\n");
        } catch (FileNotFoundException e) {
            System.err.println("Cant find file ");
            e.getStackTrace();
        }       
    }
    
    public void Part_5() {
        String online = IPaddrss.toString();
                
        ClientMusic = new ArrayList<>();
                
        if(!online.isEmpty()) {
            if(!line.isEmpty()) {
                ClientMusic.add(online + " " + line);
            } else {
                System.out.println(online + " Has no music");
            }
        } else {
            System.out.println("No Client connected");
        }
    }
    
    public void Part_6() {
        try {            
            reciveData = new byte[DEFAULT_BUFFER];
            recivePacket = new DatagramPacket(reciveData, reciveData.length);
            udpSocket.receive(recivePacket);
                
            requestSong = new String(recivePacket.getData());
                
            requestSong = requestSong.trim();
            FileInputStream finp = new FileInputStream("songlist.txt");
            InputStreamReader input = new InputStreamReader(finp);
            BufferedReader br = new BufferedReader(input);
            String ready = "";
                
            for(String line3; (line3 = br.readLine()) != null;) {
                    
                if(line3.contains(requestSong)) {
                    String[] IPaddressOfPeeps = line3.split(",");
                    IPNo = IPaddressOfPeeps[0];
                    IPNo = IPNo.replace("/", "");
                    if(!IPaddressOfPeeps[0].equals(IPNo)) {
                        ready += IPaddressOfPeeps[0] + ", ";
                    } 
                }
            }
                
        int arraySize = ready.length();
        arraySize = (arraySize * 2);
        
        sendData = new byte[arraySize];
        sendData = ready.getBytes();
        
        sendPacket = new DatagramPacket(sendData, sendData.length, IPaddrss, 4001);
        udpSocket.send(sendPacket);
        
        } catch (FileNotFoundException e) {
            System.err.println("File cant be found");
            e.getStackTrace();
        } catch (IOException e) {
            System.err.println("Cant get/send Packet");
            e.getStackTrace();
        }
    }
    
    /**
     * The method that starts the program
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Media_Server media = new Media_Server();
        media.StartUDP();
        
        while (true) {            
            media.Part_1();
            media.Part_2();
            media.Part_3();
            media.Part_4();
            media.Part_5();
            media.Part_6();
        }
        
        
        
    }
}

Comments

  • hie i am still new to this but in client part2 where you filter for .mp3 files. you added the entire files array instead of a single mp3 file. " if (file.toString().contains(".mp3")) {
    listMP3.add(Arrays.toString(files));
    }"
Sign In or Register to comment.