版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
JavaSocket編程實(shí)驗(yàn)?zāi)康模篠ocket編程是信息化時(shí)代必須掌握的一項(xiàng)技術(shù)。這次做這個(gè)Socket程序,只是簡(jiǎn)單的做了一下,了解了大致的流程。熟練掌握socket編程中的Socket類的常用方法,熟練掌握socket編程中流的使用方法實(shí)驗(yàn)原理基于客戶機(jī)/服務(wù)器的Socket通信模型:首先在服務(wù)器端建一個(gè)ServerSocket,ServerSocketserverSocket=newServerSocket(port);其中port是你自行選擇要監(jiān)聽的端口(最好是在1023以上);然后在客戶端建一個(gè)Socket,Socketsocket=newSocket(host,port);其中host是你要連接的服務(wù)器的IP地址,port是與服務(wù)器的監(jiān)聽的port相同。這樣就在兩方建立了一個(gè)連接。然后用一系列的輸入輸出流來處理通話過程即可。要想實(shí)現(xiàn)p2p,實(shí)際上就相當(dāng)于使一個(gè)程序既是服務(wù)器又是客戶端。無需兩種不同的程序。當(dāng)然這其中還會(huì)涉及到一點(diǎn)線程的問題。三、實(shí)驗(yàn)步驟:本實(shí)驗(yàn)采用的是Java語言,首先查閱了Java的Socket編程的相關(guān)資料;熟悉了解了大致的過程。開始時(shí)設(shè)計(jì)了一個(gè)一個(gè)簡(jiǎn)單的服務(wù)器/客戶端程序,在命令行下運(yùn)行,服務(wù)器端只是簡(jiǎn)單的自動(dòng)回復(fù),而不能做一個(gè)人工互動(dòng)。服務(wù)器和客戶端是獨(dú)立的兩個(gè)不同的程序,服務(wù)器啟動(dòng)界面:客戶端啟動(dòng)并發(fā)送消息后的界面:3、在原有的基礎(chǔ)上改進(jìn)成了一個(gè)多線程的聊天下棋類的程序,設(shè)計(jì)了簡(jiǎn)單的界面,由于沒有用到NAT穿透,所以只能在局域網(wǎng)內(nèi)進(jìn)行簡(jiǎn)單的交互,在本機(jī)上程序運(yùn)行界面:服務(wù)器端:客戶端:實(shí)驗(yàn)中遇到的問題本來只想用一個(gè)socket來傳遞聊天信息和下棋的坐標(biāo),但是實(shí)現(xiàn)起來過于復(fù)雜,而且在涉及到悔棋時(shí)判斷起來很麻煩,因此就用了2個(gè)線程來建立2個(gè)socket分別傳遞聊天和坐標(biāo),實(shí)現(xiàn)更加方便。程序要在監(jiān)聽與主線程之間輪流地切換,相當(dāng)于在用戶與界面交互時(shí),要保持監(jiān)聽過程地同時(shí)運(yùn)行,這樣才能實(shí)時(shí)地捕獲輸入流并顯示出來。實(shí)驗(yàn)總結(jié)簡(jiǎn)單的Socket編程,實(shí)現(xiàn)起來不復(fù)雜,但是實(shí)際上socket經(jīng)常與線程,輸入輸出流相結(jié)合,要考慮很多種情況,如何建立連接,終止連接,異常處理,斷線處理等等附:程序中的主要函數(shù)和類//多線程的run方法,兩個(gè)線程分別處理聊天信息和坐標(biāo) publicvoidrun() { try{ serverchart=newServerSocket(6312); youchart=serverchart.accept(); }catch(IOExceptione2){ //TODOAuto-generatedcatchblock e2.printStackTrace(); } while(true) { if(Thread.currentThread().getName().equals("s")) { System.out.println("進(jìn)入s線程++run??!"); try { serverchart=newServerSocket(6312); } catch(IOExceptione1) { System.out.println("Error:"+e1); } try { inchart=newDataInputStream(youchart.getInputStream()); outchart=newDataOutputStream(youchart.getOutputStream()); while(true) { Stringreadin=inchart.readUTF(); inMessage.append("服務(wù)器:"+inchart.readUTF());//讀入信息 inMessage.append("\n"); System.out.println("進(jìn)入sssssssssssssssss線程++run?。?); } } catch(Exceptione) { } } elseif(Thread.currentThread().getName().equals("t")) { System.out.println("進(jìn)入t線程++run??!"); try { server=newServerSocket(6311); } catch(IOExceptione1) { System.out.println("Error:"+e1); } try { you=server.accept(); in=newDataInputStream(you.getInputStream()); out=newDataOutputStream(you.getOutputStream()); while(true) { Stringreadin=in.readUTF(); String[]socpoint=readin.split(","); if(Integer.parseInt(socpoint[0])==1024) { System.out.println("jjjjjj"+Integer.parseInt(socpoint[0])); points.remove(points.size()-1); repaint(); } else { addPoint(newPoint(Integer.parseInt(socpoint[0]), Integer.parseInt(socpoint[1]),Boolean .valueOf(socpoint[2]) .booleanValue())); System.out.println("--=="+getPoint(points)+"\n"); repaint(); System.out.println("進(jìn)入tttttttttttt線程++run??!"); } } } catch(Exceptione) { } } } }//canvas的paint()方法,用來更新畫布,繪制棋盤和棋子 publicvoidpaint(Graphicsg){ intj=0; for(j=0;j<11;j++) { g.drawLine(10+(500/10)*j,10,10+(500/10)*j,508); g.drawLine(10,10+(500/10)*j,508,10+(500/10)*j); } Iterator<Point>i=points.iterator(); while(i.hasNext()){ Pointp=(Point)i.next(); if(p.chess==true) { g.setColor(Color.black); g.fillOval(p.x-24,p.y-24,48,48); } else { g.setColor(Color.white); g.fillOval(p.x-24,p.y-24,48,48); } } }//鼠標(biāo)點(diǎn)擊的方法,用來記錄棋子坐標(biāo),和調(diào)用paint()方法繪制棋子 publicvoidmouseClicked(MouseEvente){ if(btn==e.getSource()){ outMessage.setText("移除("+getPoint(points)+")的點(diǎn)"); points.remove(points.size()-1); try{ out.writeUTF("1024,1024,true"); }catch(IOExceptione2){ e2.printStackTrace(); }//寫入信息 try{ FileWriterout=newFileWriter(file); Iterator<Point>i=points.iterator(); while(i.hasNext()){ Pointp=(Point)i.next(); x=p.x; y=p.y; Strings=x+","+y; out.write(s); out.write("\r\n"); } out.close(); FileReaderin=newFileReader(file); System.out.println("以行為單位讀取文件內(nèi)容,一次讀一整行:"); BufferedReaderreader=newBufferedReader(newFileReader(file)); StringtempString=null; intline=1; //一次讀入一行,直到讀入null為文件結(jié)束 while((tempString=reader.readLine())!=null){ //顯示行號(hào) System.out.println("讀取line"+line+":"+tempString); String[]s=tempString.split(","); addReadpoint(newPoint(Integer.parseInt(s[0]), Integer.parseInt(s[1]),true)); line++; } intnum=0; Iterator<Point>j=Readpoint.iterator(); while(j.hasNext()){ num++; Pointp=(Point)j.next(); x=p.x; y=p.y; Strings="Readpoint中第"+num+"個(gè)"+x+","+y; System.out.println(s); } reader.close(); Readpoint.removeAll(Readpoint); }catch(IOExceptione2){ System.out.println(e2); } repaint(); } }源碼:canvas.javapackagesocket;importjavax.swing.*;importjava.awt.event.*;importjava.awt.geom.Rectangle2D;importjava.awt.image.BufferedImage;importjava.awt.*;importjava.io.*;import.*;importjava.util.ArrayList;importjava.util.Iterator;importjava.util.Vector;/*Client*/classPoint{ intx,y;booleanchess;//chess=trueblack//chess=falsewhite; Point(){ } Point(intx,inty,booleanchess){ this.x=x; this.y=y; this.chess=chess; }}classmapDOT{ intx,y;}publicclasscanvasextendsCanvasimplementsMouseListener,Runnable,ActionListener{ /** * */ privatestaticfinallongserialVersionUID=855858279582070325L; ArrayList<Point>points=null; ArrayList<Point>Readpoint=null; Strings=null,s1=null; Socketmysocket=null,mysocketchart=null; DataInputStreamin=null,inchart=null; DataOutputStreamout=null,outchart=null; //canvasServerth=newcanvasServer(); Threadthread=newThread(this,"t"); Threadtalk=newThread(this,"s"); mapDOTcoin=newmapDOT(); canvasf; JButtonbtn,connect; JTextAreainMessage=newJTextArea(34,10); JLabelmessageBox=newJLabel("消息框:"); JLabelinfor=newJLabel("info:"); JLabeladdressIP=newJLabel("地址:"); JTextFieldIP=newJTextField("",16); JButtonb=newJButton("發(fā)送"); JButtonconnectServer=newJButton("連接"); JTextFieldoutMessage; Framewindow; PanelpCenter,pEast; Rectangle2Drect; BufferedImageimage; Graphics2Dggg; intx,y,n; booleanchess; Vectorv=null; Pointp1; Filefile=newFile("chess.txt"); publiccanvas(){ points=newArrayList<Point>(); Readpoint=newArrayList<Point>(); v=newVector(); btn=newJButton("取消"); connect=newJButton("連接"); outMessage=newJTextField(16); pEast=newPanel(); pEast.add(btn); pEast.add(addressIP); pEast.add(IP); pEast.add(connectServer); pEast.add(infor); pEast.add(outMessage); //pEast.add(messageBox); pEast.add(b); b.addActionListener(this); connectServer.addActionListener(newActionListener() { publicvoidactionPerformed(ActionEvente) { s1=IP.getText(); thread.start(); talk.start(); System.out.println("kkkkkkkkkkk"+thread); System.out.println("sssssssss"+talk); } }); connect.addMouseListener(this); btn.addMouseListener(this); this.addMouseListener(this); image=newBufferedImage(200,200,BufferedImage.TYPE_INT_RGB); ggg=image.createGraphics(); rect=newRectangle2D.Double(0,0,200,200); ggg.setColor(getBackground()); ggg.fill(rect); window=newFrame("Client"); pCenter=newPanel(); pCenter.setLayout(null); pCenter.add(this); //pCenter pCenter.setBackground(Color.gray); pCenter.setLocation(0,40); this.setBounds(0,40,520,600); this.setBackground(Color.LIGHT_GRAY); FlowLayoutflow=newFlowLayout(); flow.setAlignment(FlowLayout.TRAILING); flow.setHgap(2); flow.setVgap(3); window.setLayout(flow); //window.setLayout(null); window.add(pEast); window.add(this); window.add(newJScrollPane(inMessage)); //window.add(pCenter); //window.add(pEast); window.setVisible(true); window.addWindowListener(newWindowAdapter(){ publicvoidwindowClosing(WindowEvente){ System.exit(0); } }); window.setBounds(280,90,658,620); window.validate(); } publicvoidactionPerformed(ActionEventevent) { try { outchart.writeUTF(outMessage.getText());//寫入信息 //inMessage.append("你說:"+outMessage.getText()+"\n"); //outMessage.setText(""); } catch(Exceptione) { } try { outchart.writeUTF(outMessage.getText());//寫入信息 inMessage.append("你說:"+outMessage.getText()+"\n"); outMessage.setText(""); } catch(Exceptione) { } } publicvoidrun() { while(true) { if(Thread.currentThread().getName().equals("t")) { System.out.println("進(jìn)入線程trun??!"); try { mysocket=newSocket(s1,6311); //mysocket=newSocket("localhost",6311); inMessage.append("連接成功!~~~~"+"\n"); in=newDataInputStream(mysocket.getInputStream()); out=newDataOutputStream(mysocket.getOutputStream()); while(true) { Stringreadin=in.readUTF();// // if(readin.indexOf(",")==-1)// {// inMessage.append("服務(wù)器:"+in.readUTF());//讀入信息// inMessage.append("\n");// }// //else { String[]socpoint=readin.split(","); if(Integer.parseInt(socpoint[0])==1024){ System.out.println("jjjjjj" +Integer.parseInt(socpoint[0])); points.remove(points.size()-1); repaint(); } else { addPoint(newPoint( Integer.parseInt(socpoint[0]), Integer.parseInt(socpoint[1]),Boolean .valueOf(socpoint[2]) .booleanValue())); System.out.println("--=="+getPoint(points) +"\n"); repaint(); } }// Stringreadin=in.readUTF();// // String[]socpoint=readin.split(",");// if(socpoint.length>2)// {// addPoint(newPoint(Integer.parseInt(socpoint[0]),// Integer.parseInt(socpoint[1]),Boolean.valueOf(socpoint[2]).booleanValue()));// System.out.println("--=="+getPoint(points)+"\n");// repaint();// }// else// {// inMessage.append("服務(wù)器:"+in.readUTF());//讀入信息// inMessage.append("\n");// }// try// {// Thread.sleep(500);// }// catch(InterruptedExceptione)// {// //TODOAuto-generatedcatchblock// e.printStackTrace();// } } } catch(IOExceptione) { System.out.println("無法連接!"); } } elseif(Thread.currentThread().getName().equals("s")) { System.out.println("進(jìn)入s線程++run??!"); try { mysocketchart=newSocket(s1,6312); //mysocket=newSocket("localhost",6311); inMessage.append("聊天連接成功!~~~~"+"\n"); inchart=newDataInputStream(mysocketchart.getInputStream()); outchart=newDataOutputStream(mysocketchart.getOutputStream()); while(true) { Stringreadin=inchart.readUTF();// // if(readin.indexOf(",")==-1)// { inMessage.append("服務(wù)器:"+inchart.readUTF());//讀入信息 inMessage.append("\n");// }// //else // Stringreadin=in.readUTF();// // String[]socpoint=readin.split(",");// if(socpoint.length>2)// {// addPoint(newPoint(Integer.parseInt(socpoint[0]),// Integer.parseInt(socpoint[1]),Boolean.valueOf(socpoint[2]).booleanValue()));// System.out.println("--=="+getPoint(points)+"\n");// repaint();// }// else// {// inMessage.append("服務(wù)器:"+in.readUTF());//讀入信息// inMessage.append("\n");// }// try// {// Thread.sleep(500);// }// catch(InterruptedExceptione)// {// //TODOAuto-generatedcatchblock// e.printStackTrace();// } } } catch(IOExceptione) { System.out.println("無法連接++聊天!"); } } } } publicvoidpaint(Graphicsg){ //super.paint(g); //g.drawOval(x-10,y-10,x+10,y+10); //g.drawLine(10,10,10,500); //g.drawLine(10,10,500,10); intj=0; for(j=0;j<11;j++) { g.drawLine(10+(500/10)*j,10,10+(500/10)*j,508); g.drawLine(10,10+(500/10)*j,508,10+(500/10)*j); } Iterator<Point>i=points.iterator(); while(i.hasNext()){ Pointp=(Point)i.next(); if(p.chess==true) { g.setColor(Color.black); g.fillOval(p.x-24,p.y-24,48,48); } else { g.setColor(Color.white); g.fillOval(p.x-24,p.y-24,48,48); } } } publicvoidaddPoint(Pointp){ points.add(p); } publicvoidaddReadpoint(Pointp){ Readpoint.add(p); } publicvoiddelReadpoint(Pointp){ Readpoint.removeAll(Readpoint); } publicStringgetPoint(ArrayList<Point>points){ x=points.get(points.size()-1).x; y=points.get(points.size()-1).y; chess=points.get(points.size()-1).chess; Strings=x+","+y+","+chess; returns; } publicvoidgetmap(intx,inty) { inti=0; //intj=0; for(i=0;i<6;i++) { if(10+25*i-12.5<x) coin.x=i; } for(i=0;i<11;i++) { if(10+25*i-12.5<x) coin.y=i; } System.out.println("coin中的值為"+"cion.x="+coin.x+"coin.y="+coin.y); coin.x=10+25*coin.x-12; coin.y=10+25*coin.y-12; System.out.println("在此點(diǎn)畫圓~~"+"cion.x="+coin.x+"coin.y="+coin.y); } /* *publicStringPoint(ArrayList<Point>points){ * *return"x::"+points+",y="+y;} */ @Override publicvoidmouseClicked(MouseEvente){ //Iterator<Point>i=points.iterator(); if(btn==e.getSource()){ outMessage.setText("移除("+getPoint(points)+")的點(diǎn)"); points.remove(points.size()-1); try{ out.writeUTF("1024,1024,true"); }catch(IOExceptione2){ //TODOAuto-generatedcatchblock e2.printStackTrace(); }//寫入信息// try{ FileWriterout=newFileWriter(file); Iterator<Point>i=points.iterator(); while(i.hasNext()){ Pointp=(Point)i.next(); x=p.x; y=p.y; Strings=x+","+y; out.write(s); out.write("\r\n"); } out.close(); //ArrayList<Point>Readpoint=null; FileReaderin=newFileReader(file); System.out.println("以行為單位讀取文件內(nèi)容,一次讀一整行:"); BufferedReaderreader=newBufferedReader(newFileReader(file)); StringtempString=null; intline=1; //一次讀入一行,直到讀入null為文件結(jié)束 while((tempString=reader.readLine())!=null){ //顯示行號(hào) System.out.println("讀取line"+line+":"+tempString); String[]s=tempString.split(","); addReadpoint(newPoint(Integer.parseInt(s[0]), Integer.parseInt(s[1]),false)); line++; } intnum=0; Iterator<Point>j=Readpoint.iterator(); while(j.hasNext()){ num++; Pointp=(Point)j.next(); x=p.x; y=p.y; Strings="Readpoint中第"+num+"個(gè)"+x+","+y; System.out.println(s); } reader.close(); Readpoint.removeAll(Readpoint); }catch(IOExceptione2){ System.out.println(e2); } repaint(); } } @Override publicvoidmousePressed(MouseEvente){ //TODOAuto-generatedmethodstub f=(canvas)(e.getSource()); // getmap(e.getX(),e.getY()); //f.addPoint(newPoint(coin.x,coin.y)); intflag=0; inti=0; intx1=0; inty1=0; //intj=0; for(i=0;i<12;i++) { if((10+50*i-25)>e.getX()) { if(e.getX()>(10+50*(i-1)-25)) x1=i; } } for(i=0;i<11;i++) { if((10+50*i-25)>e.getY()) { if(e.getY()>(10+50*(i-1)-25)) y1=i; } } System.out.println("coin中的值為"+"cion.x="+x1+"coin.y="+y1); x1=10+50*x1-50; y1=10+50*y1-50; //f.addPoint(newPoint(x1,y1,false)); try{ FileReaderin=newFileReader(file); System.out.println("以行為單位讀取文件內(nèi)容,一次讀一整行:"); BufferedReaderreader=newBufferedReader(newFileReader(file)); StringtempString=null; intline=1; //一次讀入一行,直到讀入null為文件結(jié)束 while((tempString=reader.readLine())!=null){ //顯示行號(hào) System.out.println("line"+line+":"+tempString); line++; String[]socpoint=tempString.split(","); if(socpoint[0].equals(String.valueOf(x1))&&socpoint[1].equals(String.valueOf(y1))) { flag++; JOptionPane.showMessageDialog(null,"請(qǐng)不要重復(fù)下子??!","提示",JOptionPane.ERROR_MESSAGE); } } reader.close(); }catch(FileNotFoundExceptione3){ //TODOAuto-generatedcatchblock e3.printStackTrace(); }catch(IOExceptione2){ //TODOAuto-generatedcatchblock e2.printStackTrace(); } if(flag==0) { f.addPoint(newPoint(x1,y1,false)); } try{ out.writeUTF(getPoint(points)); }catch(IOExceptione2){ //TODOAuto-generatedcatchblock e2.printStackTrace(); }//寫入信息// inMessage.append("你說:"+getPoint(points)+"\n");// String[]socpoint=getPoint(points).split(",");// addPoint(newPoint(Integer.parseInt(socpoint[0]),// Integer.parseInt(socpoint[1]),Boolean.valueOf(socpoint[2]).booleanValue()));// // repaint(); //f.addPoint(newPoint(e.getX(),e.getY())); x=e.getX(); y=e.getY(); outMessage.setText("鼠標(biāo)點(diǎn)擊("+x+","+y+")"); try{ FileWriterout=newFileWriter(file); Iterator<Point>j=points.iterator(); while(j.hasNext()){ Pointp=(Point)j.next(); x=p.x; y=p.y; Strings=x+","+y; out.write(s); out.write("\r\n"); } out.close(); FileReaderin=newFileReader(file); System.out.println("以行為單位讀取文件內(nèi)容,一次讀一整行:"); BufferedReaderreader=newBufferedReader(newFileReader(file)); StringtempString=null; intline=1; //一次讀入一行,直到讀入null為文件結(jié)束 while((tempString=reader.readLine())!=null){ //顯示行號(hào) System.out.println("line"+line+":"+tempString); line++; } reader.close(); }catch(IOExceptione1){ System.out.println(e1); } repaint(); } @Override publicvoidmouseReleased(MouseEvente){ //TODOAuto-generatedmethodstub } @Override publicvoidmouseEntered(MouseEvente){ //TODOAuto-generatedmethodstub } @Override publicvoidmouseExited(MouseEvente){ //TODOAuto-generatedmethodstub } publicstaticvoidmain(Stringargs[]){ newcanvas(); }}canvasServer.javapackagesocket;importjavax.swing.*;importjava.awt.event.*;importjava.awt.geom.Rectangle2D;importjava.awt.image.BufferedImage;importjava.awt.*;importjava.io.*;import.*;importjava.util.ArrayList;importjava.util.Iterator;importjava.util.Vector;/*Server*/publicclasscanvasServerextendsCanvasimplementsMouseListener,Runnable,ActionListener{ /** * */ privatestaticfinallongserialVersionUID=855858279582070325L; ArrayList<Point>points=null; ArrayList<Point>Readpoint=null; ServerSocketserver=null,serverchart=null; Socketyou,youchart; DataOutputStreamout=null,outchart=null; DataInputStreamin=null,inchart=null; Threadthread=newThread(this,"t"); Threadtalk=newThread(this,"s"); Strings2; mapDOTcoin=newmapDOT(); canvasServerf; JButtonbtn,connect; JTextAreainMessage=newJTextArea(34,10); JLabelmessageBox=newJLabel("消息框:"); JLabelinfor=newJLabel("info:"); JButtonb=newJButton("發(fā)送"); JTextFieldoutMessage; Framewindow; PanelpCenter,pEast; Rectangle2Drect; BufferedImageimage; Graphics2Dggg; intx,y,n; booleanchess; Vectorv=null; Pointp1; Filefile=newFile("chess.txt"); publiccanvasServer(){ points=newArrayList<Point>(); Readpoint=newArrayList<Point>(); v=newVector(); btn=newJButton("取消"); outMessage=newJTextField(19); pEast=newPanel(); pEast.add(btn); pEast.add(infor); pEast.add(outMessage); //pEast.add(messageBox); pEast.add(b); b.addActionListener(this); btn.addMouseListener(this); this.addMouseListener(this); setBackground(Color.white); setBackground(Color.white); image=newBufferedImage(200,200,BufferedImage.TYPE_INT_RGB); ggg=image.createGraphics(); rect=newRectangle2D.Double(0,0,200,200); ggg.setColor(getBackground()); ggg.fill(rect); window=newFrame("Server"); pCenter=newPanel(); pCenter.setLayout(null); pCenter.add(this); //pCenter pCenter.setBackground(Color.gray); pCenter.setLocation(0,40); this.setBounds(0,40,520,600); this.setBackground(Color.LIGHT_GRAY); FlowLayoutflow=newFlowLayout(); flow.setAlignment(FlowLayout.TRAILING); flow.setHgap(2); flow.setVgap(3); window.setLayout(flow); //window.setLayout(null); window.add(pEast); window.add(this); window.add(newJScrollPane(inMessage)); //window.add(pCenter); //window.add(pEast); window.setVisible(true); window.addWindowListener(newWindowAdapter(){ publicvoidwindowClosing(WindowEvente){ System.exit(0); } }); window.setBounds(280,90,658,620); window.validate(); thread.start(); talk.start(); System.out.println("kkkkkkkkkkk"+thread); System.out.println("sssssssss"+talk); } publicvoidactionPerformed(ActionEventevent) { try { outchart.writeUTF(outMessage.getText());//寫入信息 //inMessage.append("你說:"+outMessage.getText()+"\n"); //outMessage.setText(""); } catch(Exceptione) { } try { outchart.writeUTF(outMessage.getText());//寫入信息 inMessage.append("你說:"+outMessage.getText()+"\n"); outMessage.setText(""); } catch(Exceptione) { } } publicvoidrun() { try{ serverchart=newServerSocket(6312); youchart=serverchart.accept(); }catch(IOExceptione2){ //TODOAuto-generatedcatchblock e2.printStackTrace(); } while(true) { if(Thread.currentThread().getName().equals("s")) { System.out.println("進(jìn)入s線程++run??!"); try { serverchart=newServerSocket(6312); } catch(IOExceptione1) { System.out.println("Error:"+e1); } try { inchart=newDataInputStream(youchart.getInputStream()); outchart=newDataOutputStream(youchart.getOutputStream()); while(true) { Stringreadin=inchart.readUTF(); inMessage.append("服務(wù)器:"+inchart.readUTF());//讀入信息 inMessage.append("\n"); System.out.println("進(jìn)入sssssssssssssssss線程++run??!"); } } catch(Exceptione) { } } elseif(Thread.currentThread().getName().equals("t")) { System.out.println("進(jìn)入t線程++run??!"); try { server=newServerSocket(6311); } catch(IOExceptione1) { System.out.println("Error:"+e1); } try { you=server.accept(); in=newDataInputStream(you.getInputStream()); out=newDataOutputStream(you.getOutputStream()); while(true) { Stringreadin=in.readUTF(); String[]socpoint=readin.split(","); if(Integer.parseInt(socpoint[0])==1024) { System.out.println("jjjjjj"+Integer.parseInt(socpoint[0])); points.remove(points.size()-1); repaint(); } else { addPoint(newPoint(Integer.parseInt(socpoint[0]), Integer.parseInt(socpoint[1]),Boolean .valueOf(socpoint[2]) .booleanValue())); System.out.println("--=="+getPoint(points)+"\n"); repaint(); System.out.println("進(jìn)入tttttttttttt線程++run??!"); } } } catch(Exceptione) { } } } } publicvoidpaint(Graphicsg){ intj=0; for(j=0;j<11;j++) { g.drawLine(10+(500/10)*j,10,10+(500/10)*j,508); g.drawLine(10,10+(500/10)*j,508,10+(500/10)*j); } Iterator<Point>i=points.iterator(); while(i.hasNext()){ Pointp=(Point)i.next(); if(p.chess==true) { g.setColor(Color.black); g.fillOval(p.x-24,p.y-24,48,48); } else { g.setColor(Color.white); g.fillOval(p.x-24,p.y-24,48,48); } } } publicvoidaddPoint(Pointp){ points.add(p); } publicvoidaddReadpoint(Pointp){ Readpoint.add(p); } publicvoiddelReadpoint(Pointp){ Readpoint.removeAll(Readpoint); } publicStringgetPoint(ArrayList<Point>points){ x=points.get(points.size()-1).x; y=points.get(points.size()-1).y; chess=points.get(points.size()-1).chess; Strings=x+","+y+","+chess; returns; } publicvoidgetmap(intx,inty) { inti=0; //intj=0; for(i=0;i<6;i++) { if(10+25*i-12.5<x) coin.x=i; } for(i=0;i<11;i++) { if(10+25*i-12.5<x) coin.y=i; } System.out.println("coin中的值為"+"cion.x="+coin.x+"coin.y="+coin.y); coin.x=10+25*coin.x-12; coin.y=10+25*coin.y-12; System.out.println("在此點(diǎn)畫圓~~"+"cion.x="+coin.x+"coin.y="+coin.y); } /* *publicStringPoint(ArrayList<Point>points){ * *return"x::"+points+",y="+y;} */ @Override publicvoidmouseClicked(MouseEvente){ if(btn==e.getSource()){ outMessage.setText("移除("+getPoint(points)+")的點(diǎn)"); points.remove(points.size()-1); try{ out.writeUTF("1024,1024,true"); }catch(IOExceptione2){ e2.printStackTrace(); }//寫入信息 try{ FileWriterout=newFileWriter(file); Iterator<Point>i=points.iterator(); while(i.hasNext()){ Pointp=(Point)i.next(); x=p.x; y=p.y; Strings=x+","+y; out.write(s); out.write("\r\n"); } out.close(); FileReaderin=newFileReader(file); System.out.println("以行為單位讀取文件內(nèi)容,一次讀一整行:"); BufferedReaderreader=newBufferedReader(newFileReader(file)); StringtempString=null; intline=1; //一次讀入一行,直到讀入null為文件結(jié)束 while((tempString=reader.readLine())!=null){ //顯示行號(hào) System.out.println("讀取line"+line+":"+tempString); String[]s=tempString.split(","); addReadpoint(newPoint(Integer.parseInt(s[0]), Integer.parseInt(s[1]),true)); line++; } intnum=0; Iterator<Point>j=Readpoint.iterator(); while(j.hasNext()){ num++; Pointp=(Point)j.next(); x=p.x; y=p.y; Strings="Readpoint中第"+num+"個(gè)"+x+","+y; System.out.println(s); } reader.close(); Readpoint.removeAll(Readpoint); }catch(IOExceptione2){ System.out.println(e2); } repaint(); } } @Override publicvoidmousePressed(MouseEvente){ //TODOAuto-generatedmethodstub f=(canvasServer)(e.getSource()); // getmap(e.getX(),e.getY()); //f.addPoint(newPoint(coin.x,coin.y)); intflag=0; inti=0; intx1=0; inty1=0; //intj=0; for(i=0;i<12;i++) { if((10+50*i-25)>e.getX()) { if(e.getX()>(10+50*(i-1)-25)) x1=i; } } for(i=0;i<11;i++) { if((10+50*i-25)>e.getY()) { if(e.getY()>(10+50*(i-1)-25)) y1=i; } } System.out.println("coin中的值為"+"cion.x="+x1+"coin.y="+y1); x1=10+50*x1-50; y1=10+50*y1-50; //JOptionPane.showMessageDialog(null,"請(qǐng)不要??!","提示",JOptionPane.ERROR_MESSAGE); try{ FileReaderin=newFileReader(file); System.out.println("以行為單位讀取文件內(nèi)容,一次讀一整行:"); BufferedReaderreader=newBufferedReader(newFileReader(file)); StringtempString=null; intline=1; //一次讀入一行,直到讀入null為文件結(jié)束 while((tempString=reader.readLine())!=null){ //顯示行號(hào) System.out.println("line"+line+":"+tempString); line++; String[]socpoint=tempString.split(","); if(Integer.parseInt(socpoint[0])==x1&&Integer.parseInt(socpoint[1])==y1) { flag++; JOptionPane.showMessageDialog(null,"請(qǐng)不要重復(fù)下子??!","提示",JOptionPane.ERROR_MESSAGE); } } reader.close(); }catch(FileNotFoundExceptione3){ //TODOAuto-generatedcatchblock e3.printStackTrace(); }catch(IOExceptione2){ //TODOAuto-generatedcatchblo
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年度新型環(huán)保技術(shù)合作承諾合同范本4篇
- 2025版智能社區(qū)門牌制作及管理系統(tǒng)集成合同4篇
- 二零二五版智能科技專利轉(zhuǎn)讓合同補(bǔ)充協(xié)議3篇
- 數(shù)據(jù)化辦公實(shí)驗(yàn)室數(shù)據(jù)的處理與應(yīng)用
- 2025年度個(gè)人教育培訓(xùn)分期付款合同8篇
- 2025版協(xié)議書范本合同(環(huán)保產(chǎn)業(yè))2篇
- 長(zhǎng)安大學(xué)《大學(xué)外語聽說》2023-2024學(xué)年第一學(xué)期期末試卷
- 2024鐵路電氣化區(qū)段安全使用合同3篇
- 家用醫(yī)療設(shè)備為殘疾人提供個(gè)性化的康復(fù)方案
- 現(xiàn)代家庭的沖突解決與情緒管理策略
- 2025年度杭州市固廢處理與資源化利用合同3篇
- 2024年安徽省公務(wù)員錄用考試《行測(cè)》真題及答案解析
- 部編版二年級(jí)下冊(cè)《道德與法治》教案及反思(更新)
- 充電樁項(xiàng)目運(yùn)營(yíng)方案
- 退休人員出國(guó)探親申請(qǐng)書
- 高中物理競(jìng)賽真題分類匯編 4 光學(xué) (學(xué)生版+解析版50題)
- 西方經(jīng)濟(jì)學(xué)-高鴻業(yè)-筆記
- 幼兒園美術(shù)教育研究策略國(guó)內(nèi)外
- 2024屆河南省五市高三第一次聯(lián)考英語試題及答案
- 孕婦學(xué)校品管圈課件
- 《愿望的實(shí)現(xiàn)》交流ppt課件2
評(píng)論
0/150
提交評(píng)論