现在突然要用到SOCKET,反而觉得自己是记不住了,还希望各位
提供一下,小弟是十分感谢了,希望是多线程的带GUI的例子,在
下在这里先谢谢各位了

解决方案 »

  1.   

    import java.net.*;
    import java.io.*;import javax.swing.JOptionPane;public class ThreadReceive extends Thread{

    private int port;

    Socket server = new Socket(InetAddress.getLocalHost(),port);

    private DataInputStream in = null;

    private String fileName;

    public void setPort(int p){

    port=p;

    }

    public void setFileName(String fileName){

    this.fileName=fileName;

    }

    public ThreadReceive(Socket s) throws IOException{

    server = s; start();
    }


    @SuppressWarnings("deprecation")
    public void run(){

    while(true){

    try {

    System.out.println("监听中...");

    if(JOptionPane.showConfirmDialog(null,"要接收文件吗?","确认",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE)==JOptionPane.YES_OPTION){

    fileName = in.readLine();

    File file = new File("c:\\" + fileName);

    file.createNewFile();     RandomAccessFile raf=new RandomAccessFile(file,"rw");

    byte[] buf=new byte[2048];       int num=in.read(buf);       while(num!=(-1)){//是否读完所有数据          raf.write(buf,0,num);//将数据写往文件          raf.skipBytes(num);//顺序写文件字节          num=in.read(buf);//继续从网络中读取文件       }
          
          in.close();       raf.close();
    }

    } catch (IOException e) {
    // TODO 自动生成 catch 块
    e.printStackTrace();
    }
    }
    }}这是我的接收端代码,大虾帮偶看看
      

  2.   


    有偿转让Java即时通信工具源程序及设计文档可以学习到专业级的多线程、Socket、Swing图形等Java技术。其中包括设计文档,更适合学生用于毕业设计
    有意者与[email protected]联系程序在这里
    http://tomore.com/1/38258.html