does tcp server accept udp client and how?

Recommended Answers

All 41 Replies

Never tried it. Write a small test program and see what happens.

i tried to define a socket in a button and when i click it just stop

Please post the error messages.

How do you define a socket in a button?

this is the code for defining the socket

private void button1MouseClicked(java.awt.event.MouseEvent evt) {                                     
        // TODO add your handling code here:
        try{
             this.textField2.setText(textField1.getText());
      Socket d=new Socket("127.0.0.1",3000);
 this.textField2.setText("hello");

}
catch(Exception e){
}

there is no error message apper but the next print statement does not apper

the next print statement does not apper

Where is the print statement you are talking about?

If you define a variable inside of a method (like Socket d) when the method exits, the variable's definition will be gone.

the statement that i talk about is

this.textField2.setText("hello");

when i click on the button the print on the text2Field2

Are you saying that the contents of textField2 is not set to the String: "Hello"?
Is that statement executed? Add a println just before the call to setText to see if the statement is executed by printing out a message.

How can you see the contents of textField2? Is that variable being shown in a GUI?

yes textField2 is not set to string hello
yes i use GUI

Does the println I asked you to put before the setText() statement print out?

the print before the defenition of the socket is appered but the print after the defenition of the socket doesnot appered

Is there a ServerSocket waiting at that address for a connection?

Can you write a small simple program that compiles, executes and shows your problem?

the print statement before the defenition of the socket appered but the print statement after the defenition of the socket doesnot appered

yes there is a serversocket waiting at the address you can try to create an applet with a jbutton and but the following code

try{
            // this.textField2.setText(textField1.getText());
      Socket d=new Socket("127.0.0.1",3000);
 this.textField2.setText("hello");

}
catch(Exception et){
    
}

and you will see what is my problem

Is there a ServerSocket waiting at that address for a connection?

Can you write a small simple program that compiles, executes and shows your problem?

You have not posted enough code to show what is causing the problem.

Can you write a small simple program that compiles, executes and shows your problem?

You have not posted enough code to show what is causing the problem.

first thank you for your help if you try to create an applet and jbutton and by click on the button you will define a socket

private void button1MouseClicked(java.awt.event.MouseEvent evt) {                                     
        // TODO add your handling code here:
        try{
            // this.textField2.setText(textField1.getText());
      Socket d=new Socket("127.0.0.1",3000);
 this.textField2.setText("hello");

}
catch(Exception et){
    
}
    }

and you will see what the problem is

If you want me to test your code, please make a small simple program that compiles, executes and shows your problem?

What you posted will not compile or execute.


You should add a call to the printStackTrace method in the catch block so that you will get the full text of any error messages. The code that you posted ignores any exceptions.

Add: et.printStackTrace() inside of the catch block.

package testa;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.DatagramSocket;
import java.net.Socket;

/**
 *
 * @author ali
 */
public class NewJApplet extends javax.swing.JApplet {

    /**
     * Initializes the applet NewJApplet
     */
    @Override
    public void init() {
        System.out.println("you entered");
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(NewJApplet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(NewJApplet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(NewJApplet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(NewJApplet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the applet
         */
        try {
            java.awt.EventQueue.invokeAndWait(new Runnable() {

                public void run() {
                    initComponents();
                }
            });
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * This method is called from within the init() method to initialize the
     * form. WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        textField1 = new java.awt.TextField();
        button1 = new java.awt.Button();
        textField2 = new java.awt.TextField();

        setBackground(new java.awt.Color(255, 153, 255));

        jLabel1.setText("الاسم الرباعي للطالب");
        jLabel1.setName("label1");

        textField1.setName("text1");
        textField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                textField1ActionPerformed(evt);
            }
        });
        textField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                textField1KeyReleased(evt);
            }
        });

        button1.setLabel("button1");
        button1.setName("button1");
        button1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                button1MouseClicked(evt);
            }
        });
        button1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button1ActionPerformed(evt);
            }
        });

        textField2.setName("text2");
        textField2.setText("text2");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(27, 27, 27)
                        .addComponent(textField1, javax.swing.GroupLayout.PREFERRED_SIZE, 193, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(31, 31, 31)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(51, 51, 51)
                        .addComponent(button1, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(68, 68, 68)
                        .addComponent(textField2, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(textField1, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(26, 26, 26)
                .addComponent(textField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(20, 20, 20)
                .addComponent(button1, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(78, Short.MAX_VALUE))
        );
    }// </editor-fold>                        

    private void textField1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
    }                                          

    private void button1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
try{
     /*   Socket s=new Socket("127.0.0.1",3000); 
     DataInputStream   in=new DataInputStream(s.getInputStream());
 DataOutputStream out=new DataOutputStream(s.getOutputStream());
 out.writeUTF("hello");
 this.jLabel1.setText("hello");
*/
}
catch(Exception et){
    
}
    }                                       

    private void button1MouseClicked(java.awt.event.MouseEvent evt) {                                     
        // TODO add your handling code here:
        try{
            // this.textField2.setText(textField1.getText());
      Socket d=new Socket("127.0.0.1",3000);
 this.textField2.setText("hello");

}
catch(Exception et){
    
}
    }                                    

    private void textField1KeyReleased(java.awt.event.KeyEvent evt) {                                       
this.textField2.setText(textField1.getText());        // TODO add your handling code here:
    }                                      

    // Variables declaration - do not modify                     
    private java.awt.Button button1;
    private javax.swing.JLabel jLabel1;
    private java.awt.TextField textField1;
    private java.awt.TextField textField2;
    // End of variables declaration                   
}

Do you get any error messages printed when you add the printStackTrace method?

For example you need one at line 171

no i didinot get any error

If you do not get an exception at the Socket statement, then the println statement after the Socket statement should print out a message.
So either the Socket statement throws an exception and the printStackTrace() statement in the catch block prints out a message or the println statement after the Socket statement prints out a message.
One or the other MUST execute and print out a message.

thank you for your help the print statement is now apppered

What changed between the time when it did not appear and now when it does appear?

when i run the applet without displaying it on web browser it is working but now it doesnot run when i display it in web browser so how can i make it run

What errors are you getting? Look in the browser's Java console for the error messages.

how i can open the jjava console

One of the browser's menu items or options may do it.
Or go to the OS's Control panel, click on Java and find a setting somewhere there.

no error appered

that is strange. Normally when a program fails, it prints out an error message,

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.