soket通讯

soket通讯

使用socket,实现从客户端传一文件到服务器端,并在服务器端控制台打印出来,文件为:D:/data.txt 内容为:hello!
服务器和客户端都在本地


While(true){}`````到底有什么用
这种使用是为了保证它中的程序一直运行着   能够在任何时候获得内容并执行(传递。。。。。)
举例:在通讯中如果不使用就成为了点对点的通讯了(SOCKET   通讯中)
可以试一下,去掉后,只能处理一个客户的信息。
客户端使用线程,读取大的文件的时候,可以并发。
服务端使用线程,可收到多个客户的请求。

package com.one;

import java.net.*;
import java.io.*;

public class Server {
 private ServerSocket server;
 private Socket you;
 private receive rec;

 public Server() {
  try {
   server = new ServerSocket(2007);
   System.out.println("服务器运行中,监听端口:2007...... ");
   while (true) {
    you = server.accept();
    if (you != null) {
     System.out.println("有客户连接启动接收线程... ");
     new Thread(new receive(you)).start();
     System.out.println("接收文件内容如下: ---> 服务器继

续监听中... ");
    }
   }
  } catch (Exception e) {
   System.out.println("服务端运行出错! ");
  }
 }

 public static void main(String args[]) {
  new Server();
 }
}

class receive implements Runnable {
 private File files;
 private DataInputStream din = null;
 private DataOutputStream dout = null;
 private Socket mySock = null;
 private int str = 1;

 public receive(Socket you) {
  this.mySock = you;
  // files = new File( "d://data.txt ");
 }

 public void run() {
  try {
   din = new DataInputStream(mySock.getInputStream());
   dout = new DataOutputStream(mySock.getOutputStream());

   while (true) {
    if ((str = din.readInt()) == 0)
     break;
    System.out.println(" " + din.readUTF());
   }
   clears();
  } catch (Exception e) {
   System.out.println("接收出错! ");
  } finally {
   clears();
  }

 }

 void clears() {
  try {
   dout.close();
   din.close();
   mySock.close();
  } catch (Exception e) {
   System.out.println("关闭出错 ");
  }
 }

}

---------------------
package com.one;

import java.net.Socket;

public class Client {
 private Socket you;
 SendData sends;

 public Client() {
  try {
   you = new Socket("localhost", 2007);
   System.out.println("连接服务器监听端口:2007...... ");
   if (you != null) {
    System.out.println("连接成功,启动发送线程... ");
    new Thread(new SendData(you)).start();
    System.out.println("发送完毕!关闭线程... ");
   }
  } catch (Exception e) {
   System.out.println("服务端运行出错! ");
  }
 }

 public static void main(String args[]) {
  new Client();
 }
}
-----------------------
package com.one;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.net.Socket;

class SendData implements Runnable {
 private File files;
 private BufferedReader bin = null;
 private DataInputStream din = null;
 private DataOutputStream dout = null;
 private Socket mySock = null;
 private String data = " ";

 public SendData(Socket you) {
  this.mySock = you;
  files = new File("d://data.txt ");
 }

 public void run() {
  try {
   bin = new BufferedReader(new InputStreamReader(new FileInputStream(
     files)));
   din = new DataInputStream(mySock.getInputStream());
   dout = new DataOutputStream(mySock.getOutputStream());
   while ((data = bin.readLine()) != null) {
    dout.writeInt(1);
    dout.writeUTF(data);
   }
   dout.writeInt(0);
   clears();
  } catch (Exception e) {
  } finally {
   clears();
  }

 }

 void clears() {
  try {
   bin.close();
   dout.close();
   din.close();
   mySock.close();
  } catch (Exception e) {
   System.out.println("关闭出错 ");
  }
 }

}
-------------------------

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值