this is my code:
import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class Teacher1 extends javax.swing.JFrame implements Runnable{
String select;
private File mediaFile ;
private URL fileURL;
String filename;
int i=0;
String aa,r,reem;
String a="";
URL u;
Vector name=new Vector();
Vectorurl=new Vector();
private InetAddress ip;
private DatagramSocket sendSocket;
private DatagramSocket receiveSocket;
int std_port;
int port=5000;
/** Creates new form Teacher1 */
// public Vector name,url;
public Teacher1() {
try
{
sendSocket = new DatagramSocket();
receiveSocket = new DatagramSocket(6666);
}
catch(Exception e)
{
System.out.println(e);
}
initComponents();
// show();
}
public void paint(Graphics g){
super.paint(g);
g.setColor(Color.BLACK);
g.fillArc(-250,-30,600,700,0,360);
g.setColor(new Color(195, 57, 0));
g.drawArc(-250,-30,598,700,0,360);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
addF = new javax.swing.JButton();
removeF = new javax.swing.JButton();
start = new javax.swing.JButton();
list1 = new java.awt.List();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
getContentPane().setLayout(null);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setBackground(new java.awt.Color(255, 186, 0));
jPanel1.setLayout(null);
jPanel1.setBackground(new java.awt.Color(195, 57, 0));
jPanel1.setBorder(new javax.swing.border.CompoundBorder());
addF.setBackground(new java.awt.Color(255, 186, 0));
addF.setFont(new java.awt.Font("Comic Sans MS", 1, 12));
addF.setText("Add Files");
addF.setBorder(new javax.swing.border.EmptyBorder(new java.awt.Insets(1, 1, 1, 1)));
addF.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
addFActionPerformed(evt);
}
});
jPanel1.add(addF);
addF.setBounds(0, 190, 140, 40);
removeF.setBackground(new java.awt.Color(255, 186, 0));
removeF.setFont(new java.awt.Font("Comic Sans MS", 1, 12));
removeF.setBorder(new javax.swing.border.EmptyBorder(new java.awt.Insets(1, 1, 1, 1)));
removeF.setText("Remove File");
removeF.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
removeFActionPerformed(evt);
}
});
jPanel1.add(removeF);
removeF.setBounds(0, 280, 140, 40);
start.setBackground(new java.awt.Color(255, 186, 0));
start.setFont(new java.awt.Font("Comic Sans MS", 1, 11));
start.setText("Start Live Lecture");
start.setBorder(new javax.swing.border.EmptyBorder(new java.awt.Insets(1, 1, 1, 1)));
start.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
startActionPerformed(evt);
}
});
list1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
list1ActionPerformed(evt);
}
});
jPanel1.add(start);
start.setBounds(0, 380, 140, 40);
getContentPane().add(jPanel1);
jPanel1.setBounds(450, 0, 140, 570);
list1.setBackground(new java.awt.Color(230, 215, 172));
getContentPane().add(list1);
list1.setBounds(40, 120, 180, 370);
jLabel1.setFont(new java.awt.Font("Comic Sans MS", 1, 14));
jLabel1.setText("List Of Files");
getContentPane().add(jLabel1);
jLabel1.setBounds(20, 60, 170, 50);
jLabel2.setIcon(new javax.swing.ImageIcon("C:\\Documents and Settings\\Heyam\\My Documents\\e_learning.jpg"));
//getContentPane().add(jLabel2);
jPanel1.add(jLabel2);
jLabel2.setBounds(-3, 40, 150, 100);
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width-600)/2, (screenSize.height-600)/2, 600, 600);
}
// </editor-fold>
private void startActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
new teacher_Live().show();
}
private void list1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
select=list1.getSelectedItem();
}
private void removeFActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
System.out.print("*******"+select);
list1.remove(select);
}
private void addFActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
mediaFile = getFile();
if ( mediaFile != null ) {
// obtain URL from file
try {
fileURL = mediaFile.toURL();
//System.out.println(mediaFile);
}
catch ( MalformedURLException badURL ) {
badURL.printStackTrace();
JOptionPane.showMessageDialog( null, "Error"
);
}
list1.add(mediaFile.getName());
listing();
System.out.print(aa +"wwwww" +u);
} //end if
}// end actionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
Teacher1 application = new Teacher1();
new Teacher1().setVisible(true);
//application.waitForPackets();
}
// Variables declaration - do not modify
private javax.swing.JButton addF;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JPanel jPanel1;
private java.awt.List list1;
private javax.swing.JButton removeF;
private javax.swing.JButton start;
// End of variables declaration
// get file from computer
public File getFile()
{
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(
JFileChooser.FILES_ONLY );
int result = fileChooser.showOpenDialog( this );
if ( result == JFileChooser.CANCEL_OPTION )
return null;
else
return fileChooser.getSelectedFile();
}
public void listing(){
if ( mediaFile != null ) {
// obtain URL from file
try {
fileURL = mediaFile.toURL();
filename=mediaFile.getName();
}
catch ( MalformedURLException badURL ) {
badURL.printStackTrace();
JOptionPane.showMessageDialog( null, "Error"
);
}
}//end if
name.add(i,filename);
url.add(i,fileURL);
aa=(String)name.get(i);
u=(URL)url.get(i);
a= a +"the file is>>>>"+ aa +"@"+u+"#";
System.out.print(a +"\n");
i++;
}//method listing
/************************************************/
public void run()
{
InetAddress std_address;
try
{
while(true)
{
byte[] receivedData = new byte[1024];
DatagramPacket receivedPacket = new DatagramPacket(receivedData,receivedData.length);
receiveSocket.receive(receivedPacket);
std_address = receivedPacket.getAddress();
ip = std_address;
System.out.println(""+std_address);
String s = new String(receivedPacket.getData()).trim();
System.out.println(s);
//StringTokenizer tok=new StringTokenizer(s);
//s=tok.nextToken();
if(s.compareTo("@Ports@")==0)
{
std_port = Integer.parseInt(s);
}
/*if(s.compareTo("@ListRequest@")==0)
sendFileNames(rcvAddress,s);
if(s.compareTo("@LiveLecture@")==0)
{
boolean x = liveLect.getLiveLectuteBoolean();
System.out.println("hi xxxxxxxxx "+x);
if(x==true)
{
s=tok.nextToken();
String k = "Student "+s+" trys to connect with live lecture";
k+= "\nDo you want to accept him ?";
int selection = JOptionPane.showConfirmDialog(null,k,"Accept",JOptionPane.OK_CANCEL_OPTION,JOptionPane.INFORMATION_MESSAGE,null);
if(selection == JOptionPane.OK_OPTION)
{
liveLect.studentList.addItem(s);
//listToAddress.addItem(""+rcvAddress);
sendState(rcvAddress,"@LiveLecture@yes yes");
s=tok.nextToken();
int vidPort = Integer.parseInt(s);
s=tok.nextToken();
int audPort = Integer.parseInt(s);
liveLect.Transmit(vidPort);
liveLect.audioTransmitter(audPort);
}
else
sendState(rcvAddress,"@LiveLecture@yes no");
}
else
{
sendState(rcvAddress,"@LiveLecture@no");
}
}
if(s.compareTo("@Intersection@")==0)
{
System.out.println("Intersection");
liveLect.allowButton.setEnabled(true);
liveLect.denyButton.setEnabled(true);
liveLect.draw( true );
liveLect.intersectLabel.setText("There is an Intersection");
}
if(s.compareTo("@play@")==0)
{
s=tok.nextToken();
String t = getURL(s,ip);
System.out.println(t);
if(t.trim().compareToIgnoreCase("") == 0)
{
sendState(rcvAddress,"@play@no");
}
else
{
sendState(rcvAddress,"@play@yes");
transmitter = new Transmitter(t, 9500, 9600);
Thread thr1 = new Thread(transmitter);
thr1.start();
}
}
if(s.compareTo("@Question@")==0)
{
//System.out.println("Intersection");
receivedData = new byte[1024];
receivedPacket = new DatagramPacket(receivedData,receivedData.length);
receiveSocket1.receive(receivedPacket);
String packRcv = new String(receivedPacket.getData()).trim();
liveLect.chatTextArea.append(packRcv+"\n");
liveLect.sendButton.setEnabled(true);
liveLect.writeArea.setEnabled(true);
}*/
}//end while
}
catch(Exception e)
{
System.out.println(e);
}
}
}//frame
Great, thanks a million. As you can see, the code tags don't work here yet, thank you Sun. Ttry using HTML directly instead.
You are binding a DatagramSocket to port 6666. Are you trying to run two instances of the program at the same time?
... and why do you have a separate sendSocket? This is not necessary, you can send and receive from the same DatagramSocket.