java udp

Discover java udp, include the articles, news, trends, analysis and practical advice about java udp on alibabacloud.com

java--Network Programming (UDP)

This morning gives you a brief introduction of TCP network communication knowledge, now for everyone to complement the knowledge of complete network programming, about UDP communication knowledge.UDP is an unreliable network protocol, so what is the use value or necessity? In some cases, the UDP protocol may become very useful. Because UDP has the speed advantage

Java UDP Multi-user Multicast Chat program

ActionListener {String msg = null; String line = System.getproperty ("Line.separator");p ublic void actionperformed (ActionEvent e) {try {msg = Messagearea.gettext (); FileOutputStream Note = new FileOutputStream ("Note.txt"); Messagearea.append ("The record has been saved in NOTE.TXT "); Note.write (Msg.getbytes ()); Messagearea.append (line); Note.close ();} catch (IOException E1) {System.out.println ("Send Failed");}}} Clear the chat record on the panel class Clearmsg implements ActionListen

Java sends UDP broadcast packets

2013-06-07 22:44 1272 people read comments (2) favorite reports [Java]View Plaincopy Import java.io.IOException; Import Java.net.DatagramPacket; Import Java.net.DatagramSocket; Import java.net.InetAddress; Import java.net.SocketException; Import java.net.UnknownHostException; Import Java.text.SimpleDateFormat; Import Java.util.Date; Import Java.util.Timer; Public class Udpsend { public void SendData ()throws SocketException,

JAVA--UDP Screen Broadcast Code

; - ImportJavax.print.attribute.standard.PrinterMessageFromOperator; - ImportJavax.swing.ImageIcon; the - Public classClientreceiverthreadextendsThread { - PrivateClientui UI; - Publicclientreceiverthread (Clientui UI) { + This. UI =UI; - } + Public voidrun () { A byte[] buf =New byte[1024 * 60]; atDatagramsocket Recvsocket =NULL; - byte[] Byterecvdata =NULL; -Bytearrayinputstream Bais =NULL; -BufferedImage BUFIMAGERECV =NULL; -ImageIcon ImageIcon =NU

Datagramsocket sending and receiving of UDP under Java

(); } //get the native IP Public Static voidGetIP ()throwsunknownhostexception {inetaddress IP=Inetaddress.getlocalhost (); System.out.println (Ip.gethostaddress ()); }}Receive PackageCn.stat.p4.ipdemo;Importjava.io.IOException;ImportJava.net.DatagramPacket;ImportJava.net.DatagramSocket;Importjava.net.SocketException; Public classJieshoudemo {/** * @paramargs *@throwsIOException*/ Public Static voidMain (string[] args)throwsIOException { while(true) {Jieshou (); } }

Java IO Review UDP

voidMain (String args[]) {Try{datagramsocket ServerSocket=NewDatagramsocket (7777); byte[] Data=New byte[1024]; Datagrampacket Packet=NewDatagrampacket (data,data.length); System.out.println ("Server started ..."); Serversocket.receive (packet); String s=NewString (data,0, Packet.getlength ()); System.out.println ("Get client:" +s); Datagramsocket Clientsocket=NewDatagramsocket (); String SendData= "Welcome to you visit"; byte[] bytes=senddata.getbytes (); InetAddress IP=packet.getad

Java UDP chat and file transfer

];//prevents the file name word from being too long (here the maximum 256 characters) Datagrampacket Packet_tip = new Datagrampacket (filetipbuf,0, Filetipbuf.length); socket.receive (Packet_tip); String Str_filetip = new String (Packet_tip.getdata (), 0,packet_tip.getlength ()), filename = str_filetip.substring (8); Ta.append ( Str_filetip + "\ n");//Receive file Length (bytes) byte[] Filelenbuf = new byte[12];//capable of transmitting 1T files Datagrampacket Packet_len = new Datagrampac Ket (f

TCP and UDP programming for Java

(BUF, 1024); //receive data, put in a datagramDs.receive (DP); //extracting data from a datagramString info =NewString (Dp.getdata (), 0, Dp.getlength ()); System.out.println ("The message Received is:" +info); }}Service side:ImportJava.net.DatagramPacket;ImportJava.net.DatagramSocket;Importjava.net.InetAddress; Public classUdpserver { Public Static voidMain (string[] args)throwsException {datagramsocket ds=NULL; Datagrampacket DP=NULL; //building a Send objectDS =NewDatagramsocket (); //packag

Java UDP Network Programming Simple example

Service-side code/** *udpclient * @author winty [email protected] * @version 2008-12-15 * *Importjava.io.*;ImportJava.net.*;class udpclient{ Public Static void Main(string[] args)throwsioexception{Datagramsocket client =NewDatagramsocket (); String Sendstr ="hello! I ' m Client ";byte[] sendbuf; SendBuf = Sendstr.getbytes (); InetAddress addr = Inetaddress.getbyname ("127.0.0.1");intPort =5050; Datagrampacket Sendpacket =NewDatagrampacket (SendBuf, sendbuf.length, addr, port); Client.send

Java establishes a UDP connection

1. Server-side Packagecom.machuang.udp;Importjava.io.IOException;ImportJava.net.DatagramPacket;ImportJava.net.DatagramSocket;Importjava.net.SocketException; Public classSERVERUDP { Public Static voidMain (string[] args)throwsIOException {datagramsocket server=NewDatagramsocket (8888); byte[] container =New byte[1024]; Datagrampacket Packet=NewDatagrampacket (container, container.length); Server.receive (packet); byte[] Receivedata =Packet.getdata (); intLen =packet.getlength (); S

Java UDP (Datagramsocket,datagrampacket) instance

Import Java.net.datagrampacket;import java.net.datagramsocket;import java.net.inetaddress;import java.net.Socket; Import Java.net.socketexception;import Java.net.unknownhostexception;import Org.junit.test;public class TestUDP {@ testpublic void Send () throws exception{string str = "Hello";D atagrampacket pack = new Datagrampacket (str.getbytes (), str. GetBytes (). Length,inetaddress.getbyname ("127.0.0.1"), 10000); Datagramsocket scoket = new Datagramsocket (); for (int i =0; iJava

Java 25-4 Network programming of UDP protocol transmission ideas

UDP transportTwo classes:Datagramsocket and Datagrampacket(see API)A: Set up the sending side, the receiving end.B: Set up the data packet.C: Call the socket's send receive method.D: Close the socket.E: The sending and receiving end is two separate running programs.UDP transmission-send-side ideasUDP sends data:A: Create A Socket send-side objectB: Create a Datagram Package (package the data)C: Call the Socket object to send a datagram packetD: Freein

JAVA--TCP and UDP

There are two types of communication protocols in an existing network: TCP and UDPTcp is a protocol designed to provide reliable, end-to-end byte-stream traffic on unreliable internet, a connection-oriented protocol in which TCP connections are byte streams rather than message flows. The TCP protocol is like making a phone call, and there is a speech and answer after the switch. Udp Provides a way for an applicati

Java method for obtaining available UDP port numbers

Java to obtain the available UDP port number. The TCP method is similar to this one. Method 1: If you do not mind obtaining the port number range, you can use the assumramsocket constructor to define 0 as its port number. The system will assign it an idle Port Number: public static DatagramSocket getRandomPort() throws SocketException {DatagramSocket s = new DatagramSocket(0);return s;} Method 2: If you w

Java--udp

: Udpclient_demopublic class Udpclient_demo {public static void main (string[] args) throws exception{//TODO auto-generated method stub datagramsocket ds = null;//Declaration Datagramsocket Object byte[] buf = new byte[1024];//definition receive data BYTE data datagrampacket DP = null;//Declaration Datagrampacket Object ds = new Datagramsocket (9000);//This client listens on 9000 port DP = new Datagrampacket ( buf,1024)///Specifies the length of the received data is 1024system.out.printl

UDP Broadcast Java

voidMain (string[] args) {//TODO auto-generated Method Stub//Broadcast implementation: broadcast by the client, server-side receiveString host = "255.255.255.255";//Broadcast Address intPort = 9999;//destination port for broadcastString message = "Test";//the string to send Try{inetaddress adds=Inetaddress.getbyname (host); Datagramsocket DS=NewDatagramsocket (); Datagrampacket DP=NewDatagrampacket (Message.getbytes (), message.length (), adds, port); while(true) {Ds.send (DP); T

Java UDP chat and file transfer

() + "\ n"); Ta_send.settext ("");} catch (Exception ex) {System.out.println (ex);}}}); /try {ip = inetaddress.getbyname (STR_IP); socket = new Datagramsocket (this.myport); socket1 = new Datagramsocket ( This.myport + 1); Socket2 = new Datagramsocket (This.myport + 2); Timer timer = new timer ();//timer, flush Receive Message timer.schedule (new mytimertask_receive (), 0, +); while (Socket!=null) {try {// byte filetipbuf[] = new BYTe[256];//prevents the file name word from being too long (up t

Java implements UDP protocol transmission Datagramsocket

voidMain (string[] args)throwsioexception{datagramsocket Client=NewDatagramsocket (); String Sendstr= "hello! I ' m Client "; byte[] sendbuf; SendBuf=sendstr.getbytes (); InetAddress Addr= Inetaddress.getbyname ("10.0.1.203"); intPort = 5050; Datagrampacket Sendpacket=NewDatagrampacket (SendBuf, sendbuf.length, addr, port); Client.send (Sendpacket); byte[] Recvbuf =New byte[100]; Datagrampacket Recvpacket=NewDatagrampacket (Recvbuf, recvbuf.length); Client.receive (Recvpac

Java UDP communication

Server-side:public class udpserver{public static void Main (string[] args) throws exception{byte buf[] = new byte[1024];Datagrampacket DP = new Datagrampacket (buf,buf.length);Datagramsocket ds = new Datagramsocket (5678);while (true) {Ds.receive (DP);System.out.println (New String (Buf,0,dp.getlength ()));}}}Client:Import java.net.*; Import java.io.*;public class udpclient{public static void Main (string[] args) throws exception{Byte[] buf = (new String ("Hello server!")). GetBytes ();Datagramp

Use UDP in Java to implement simple chat functions

,bytes.length); - //accepting packets with sockets - recevicesocket.receive (datagrampacket); - //get the IP address of the sending end -inetaddress IP =datagrampacket.getaddress (); in //converts the received message to a string -String rec =NewString (Datagrampacket.getdata ()); toSystem.out.println (ip.gethostaddress () + "message Sent:" +rec); + -}Catch(SocketException e) { the e.printstacktrace (); *}Catch(IOException e) { $ e.printstacktrace

Total Pages: 15 1 .... 6 7 8 9 10 .... 15 Go to: Go

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

not found

404! Not Found!

Sorry, you’ve landed on an unexplored planet!

Return Home
phone Contact Us
not found

404! Not Found!

Sorry, you’ve landed on an unexplored planet!

Return Home
phone Contact Us

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.