客户端:
public List<String> playlist()
{
try
{
InetAddress serverAddress = InetAddress.getByName(SERVER_NAME);
Socket clientSocket = new Socket(serverAddress, DEST_PORT);
BufferedReader BR = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()) );
String rLine;
DefaultListModel listModel = new DefaultListModel();
String reply = BR.readLine();
System.out.println("ddddddd");
while((rLine = BR.readLine()) != null)
{
System.out.println("aaaaa");
listModel.addElement(rLine);
System.out.println(rLine);
System.out.println("dhhhhh");
}
PlayList.setModel(listModel);
}
catch(Exception e) {}`enter code here`
}
服务器端:
public class MediaServer {
try {
ServerSocket svrSocket = new ServerSocket(DEST_PORT);
while(true){
System.out.println("waiting for clients...");
Socket ClientSocket = svrSocket.accept();
DataOutputStream out = new DataOutputStream(ClientSocket.getOutputStream());
BufferedReader bis = new BufferedReader(new InputStreamReader(ClientSocket.getInputStream()));
InetAddress clientIP = ClientSocket.getInetAddress();
byte[] buf = new byte[1024];
String workingDirectory = System.getProperty("user.dir");
String folder = "mySongs";
System.out.println(workingDirectory);
String absolutePath = workingDirectory + File.separator + folder;
System.out.println(absolutePath);
File mediaFolder = new File(folder);
if(mediaFolder.isDirectory())
{
File [] playList = mediaFolder.listFiles();
if(playList != null)
{
for(int i = 0; i < playList.length; i++)
{
if(!playList[i].isDirectory())
{
String fileNames = playList[i].getName();
out.writeUTF(fileNames);
System.out.print(i + fileNames);
}
}
}
} else {
// System.out.println("No Music Folder Exists");
}
//FileInputStream fis = new FileInputStream();
// String songRequest = bis.readLine();
}
}
catch(Exception e) {
}
}