复制代码
- //:NameSender.java
- //Anappletthatsendsanemailaddress
- //asadatagram,usingJava1.02.
- importjava.awt.*;
- importjava.applet.*;
- importjava.net.*;
- importjava.io.*;
- publicclassNameSenderextendsApplet
- implementsRunnable{
- privateThreadpl=null;
- privateButtonsend=newButton(
- "Addemailaddresstomailinglist");
- privateTextFieldt=newTextField(
- "typeyouremailaddresshere",40);
- privateStringstr=newString();
- privateLabel
- l=newLabel(),l2=newLabel();
- privateDatagramSockets;
- privateInetAddresshostAddress;
- privatebyte[]buf=
- newbyte[NameCollector.BUFFER_SIZE];
- privateDatagramPacketdp=
- newDatagramPacket(buf,buf.length);
- privateintvcount=0;
- publicvoidinit(){
- setLayout(newBorderLayout());
- Panelp=newPanel();
- p.setLayout(newGridLayout(2,1));
- p.add(t);
- p.add(send);
- add("North",p);
- Panellabels=newPanel();
- labels.setLayout(newGridLayout(2,1));
- labels.add(l);
- labels.add(l2);
- add("Center",labels);
- try{
- //Auto-assignportnumber:
- s=newDatagramSocket();
- hostAddress=InetAddress.getByName(
- getCodeBase().getHost());
- }catch(UnknownHostExceptione){
- l.setText("Cannotfindhost");
- }catch(SocketExceptione){
- l.setText("Cantopensocket");
- }
- l.setText("Readytosendyouremailaddress");
- }
- publicbooleanaction(Eventevt,Objectarg){
- if(evt.target.equals(send)){
- if(pl!=null){
- //pl.stop();DeprecatedinJava1.2
- Threadremove=pl;
- pl=null;
- remove.interrupt();
- }
- l2.setText("");
- //Checkforerrorsinemailname:
- str=t.getText().toLowerCase().trim();
- if(str.indexOf()!=-1){
- l.setText("Spacesnotallowedinname");
- returntrue;
- }
- if(str.indexOf(,)!=-1){
- l.setText("Commasnotallowedinname");
- returntrue;
- }
- if(str.indexOf(@)==-1){
- l.setText("Namemustinclude@");
- l2.setText("");
- returntrue;
- }
- if(str.indexOf(@)==0){
- l.setText("Namemustpreceed@");
- l2.setText("");
- returntrue;
- }
- Stringend=
- str.substring(str.indexOf(@));
- if(end.indexOf(.)==-1){
- l.setText("Portionafter@must"+
- "haveanextension,suchas.com");
- l2.setText("");
- returntrue;
- }
- //EverythingsOK,sosendthename.Geta
- //freshbuffer,soitszeroed.Forsome
- //reasonyoumustuseafixedsizerather
- //thancalculatingthesizedynamically:
- byte[]sbuf=
- newbyte[NameCollector.BUFFER_SIZE];
- str.getBytes(0,str.length(),sbuf,0);
- DatagramPackettoSend=
- newDatagramPacket(
- sbuf,100,hostAddress,
- NameCollector.COLLECTOR_PORT);
- try{
- s.send(toSend);
- }catch(Exceptione){
- l.setText("Couldntsenddatagram");
- returntrue;
- }
- l.setText("Sent:"+str);
- send.setLabel("Re-send");
- pl=newThread(this);
- pl.start();
- l2.setText(
- "Waitingforverification"+++vcount);
- }
- elsereturnsuper.action(evt,arg);
- returntrue;
- }
- //Thethreadportionoftheappletwatchesfor
- //thereplytocomebackfromtheserver:
- publicvoidrun(){
- try{
- s.receive(dp);
- }catch(Exceptione){
- l2.setText("Couldntreceivedatagram");
- return;
- }
- l2.setText(newString(dp.getData(),
- 0,0,dp.getLength()));
- }
- }///:~
复制代码
- <HTML>
- <HEAD>
- <METACONTENT="text/html">
- <TITLE>
- AddYourselftoBruceEckelsJavaMailingList
- </TITLE>
- </HEAD>
- <BODYLINK="#0000ff"VLINK="#800080"BGCOLOR="#ffffff">
- <FONTSIZE=6><P>
- AddYourselftoBruceEckelsJavaMailingList
- </P></FONT>
- Theappletonthispagewillautomaticallyaddyouremailaddresstothemailinglist,soyouwillreceiveupdateinformationaboutchangestotheonlineversionof"ThinkinginJava,"notificationwhenthebookisinprint,informationaboutupcomingJavaseminars,andnotificationaboutthe“Hands-onJavaSeminar”MultimediaCD.Typeinyouremailaddressandpressthebuttontoautomaticallyaddyourselftothismailinglist.<HR>
- <appletcode=NameSenderwidth=400height=100>
- </applet>
- <HR>
- Ifafterseveraltries,youdonotgetverificationitmeansthattheJavaapplicationontheserverishavingproblems.Inthiscase,youcanaddyourselftothelistbysendingemailto
- <AHREF="mailto:Bruce@EckelObjects.com">
- Bruce@EckelObjects.com</A>
- </BODY>
- </HTML>
欢迎光临 仓酷云 (http://ckuyun.com/) | Powered by Discuz! X3.2 |