Results 1 to 6 of 6

Thread: UDP socket client / server

  1. #1
    Join Date
    Dec 2009
    Posts
    296

    UDP socket client / server

    Hello
    I just started network programming in java, and I took my first sockets (client / server) via a UDP protocol. But that does not work. So, may be you can help me out, here is my code.
    Code:
    Public class serveurUDP {
    	
    	static int sz = 1024;
    	static byte[] buff = new byte[sz];
    	static int pt = 9332;
     
    	Public static void hand(String[] args)throws Exception {
    		DatagramSocket sc = new DatagramSocket(pt);
    String give = "";
    String message = "";
    sz = 0;
    System.out.System.out.println("Server Initialized");
    		while(true) {
    		DatagramPacket packet = new DatagramPacket(stamp, stamp.length);
    sc.receive(Package);
    System.out.System.out.println(package received from:+ Package.getAddress());
    sz = packet.getLength();
    gives = new String(package.getData(),0, Size);
    message = "hello"+ yields;
    System.out.System.out.println("Data that will be sent are:+ Message);
    DatagramPacket send = new DatagramPacket(message.getBytes(), Message.length(), Pack.getAddress(), Pack.getPort());
    sc.Send(sends);
    		}
     
    	}
     
    }

  2. #2
    Join Date
    Nov 2009
    Posts
    333

    Re: UDP socket client / server

    Hi,
    As you except the announcement, the port is already used. In effect, you create a DatagramSocket in the hand of your customer with this port, but also in the hand of your server. Since you're on the same PC, it throws an exception. Your client does not need a port to create its socket. That's what you'll specify the DatagramPacket which will make it will contact the server as it is (it however you need to know the listening port of your server).

  3. #3
    Join Date
    Dec 2009
    Posts
    296

    Re: UDP socket client / server

    Hey,
    Thanks for the advice, as you said I have updated my code, please check and if I am wrong somewhere then guide me.
    Code:
    Public class TestInetAddress {
    
    	Public static void hand(String[] args) throws IOException {
    		ServerSocket ser = new ServerSocket(300);
    Socket cl = ser.accept();
    BufferedReader rd = new BufferedReader(new InputStreamReader(customer
    .getInputStream()));
    String line;
    
    		try {
    			while ((line = rd.readLine()) != null) {
    				System.out.System.out.println(row);
    			}
    		} catch (Exception e) {
    			e.printStackTrace();
    		} finally {
    			rd.close();
    customer.close();
    ser.close();
     
    		}
     
    	}
     }

  4. #4
    Join Date
    Nov 2009
    Posts
    583

    Re: UDP socket client / server

    Hi,
    - The connected mode (similar to a telephone), using TCP. In this mode of communication, a stable connection is established between the two processes, so that the destination address is not required for each submission of data.
    - The non-connected mode (similar to a communication by mail), using the UDP protocol. This mode requires the destination address for each mailing, and no acknowledgment is given.

  5. #5
    Join Date
    Dec 2009
    Posts
    296

    Re: UDP socket client / server

    Hello,
    Level TCP or UDP is a little blurry but it's better. Otherwise thank you for the buffer I see what it is. For class server, I had not realized that this was only the server. Finally you've responded really well, thank you. (I found all of the same UDP is simpler than TCP, even though I know it is less reliable but faster)
    Code:
    BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(customer.getOutputStream()));
    But after, when I made
    wr.Write (message.getBytes ())
    but it is not working. So, is there any other method to send data over client and sever program.

  6. #6
    Join Date
    Nov 2009
    Posts
    356

    Re: UDP socket client / server

    Hello,
    You must give the ip of your server to people who want to connect, or when you have a domain name (DNS => basically a name associated with ip). Knowing that the ip is good, but not enough to connect to a server, the client must also know what port to connect (and with what protocol, TCP or UDP). From there the customer will use an InetSocketAddress (which contains an InetAddress).

Similar Threads

  1. Replies: 6
    Last Post: 22-07-2011, 10:24 PM
  2. connection to server failed socket error 10013
    By leijen in forum Windows Vista Mail
    Replies: 1
    Last Post: 25-04-2011, 11:04 AM
  3. Replies: 7
    Last Post: 16-02-2011, 08:14 PM
  4. What is socket and server socket in java?
    By Truster in forum Software Development
    Replies: 5
    Last Post: 01-02-2010, 02:08 PM
  5. Socket Client/Server
    By KACY5 in forum Software Development
    Replies: 4
    Last Post: 07-12-2009, 09:49 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,715,272,412.34185 seconds with 17 queries