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("关闭出错 ");
}
}
}
-------------------------
1万+

被折叠的 条评论
为什么被折叠?



