标签:返回 ade 读取数据 print 服务器ip 获得 hat 服务器传文件 函数
package socket;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class ServerScoket {
// 服务器端
int port = 6060;// 设置的端口号
boolean quit=false;
static int i;//分支
public static void main(String arg[]) throws InterruptedException {
// 1、客户端传文件或图片到服务器
new ServerScoket().ClientToServer();
// 2、服务器传文件或图片到客户端
String filepath = ".\\Server\\server.jpg";// 服务器中所传文件的路径
new ServerScoket().ServerToClient(filepath);
// 3、服务器客户端互聊
new ServerScoket().ServerSocketChat();
System.out.println("断开连接,聊天已结束!");
}
public void ClientToServer() {// 接受客户端上传的文件,并保存
try {
ServerSocket server = new ServerSocket(port);
System.out.println("服务器已开启");
//while (true) {
Socket socket = server.accept();
DataInputStream is = new DataInputStream(
socket.getInputStream());
OutputStream os = socket.getOutputStream();
// 得到文件名
String filename = ".\\Server\\";
filename += is.readUTF();
System.out.println("新生成的文件名为:" + filename);
FileOutputStream fos = new FileOutputStream(filename);
byte[] b = new byte[1024];
int length = 0;
while ((length = is.read(b)) != -1) {
// 把socket输入流写到文件输出流中去
fos.write(b, 0, length);
}
fos.flush();
fos.close();
is.close();
socket.close();
//}
} catch (IOException e) {
e.printStackTrace();
}
}
// 服务器传送文件或图片给客户端
public void ServerToClient(String filePath) {
// 接受客户端的下载请求,将本地文件传输给客户端
try {
ServerSocket server = new ServerSocket(port+1);
System.out.println("服务器已开启");
//while (true) {
// 选择进行传输的文件
File file = new File(filePath);
Socket socket = server.accept();
System.out.println("建立socket链接");
DataInputStream fin = new DataInputStream(
new BufferedInputStream(new FileInputStream(filePath)));
DataOutputStream fout = new DataOutputStream(
socket.getOutputStream());
// 将文件名传给客户端
fout.writeUTF(file.getName());
// System.out.println(file.getName());
fout.flush();
System.out.println("传输文件大小:" + file.length() * 1.0 / 1024
+ " KB");
int bufferSize = 8 * 1024;
byte[] buf = new byte[bufferSize];
while (true) {
//System.out.print(file.getName() + "文件正在从服务器传给客户端...\n");
int read = 0;
if (fin != null) {
read = fin.read(buf);
}
if (read == -1) {
break;
}
fout.write(buf, 0, read);
}
fout.flush();
fin.close();
socket.close();
System.out.println(file.getName() + "文件已从服务器传给客户端,保存在Client文件夹中。");
//}
} catch (Exception e) {
e.printStackTrace();
}
}
// 服务器客户端互聊
public void ServerSocketChat() throws InterruptedException {
try {
ServerSocket ss = new ServerSocket(port+2);
System.out.println("服务器已开启");
Socket s = ss.accept();
while (true) {
runner2 r = new runner2(s);// 代参构造函数构造
Thread t = new Thread(r);
t.start();
// 服务器写给客户端
OutputStream os = s.getOutputStream();
Scanner scan = new Scanner(System.in);
String str = scan.next();
String s1 = getDate() + "\t服务器说:" + str;
System.out.println(s1);
os.write(s1.getBytes());
if(quit)
{
return;
}
if ("q".equals(str)) {
os.write(str.getBytes());
t.sleep(1000);
return;// 结束聊天
}
}
// s.close();
// System.out.println("断开连接,聊天已结束!");
} catch (IOException e) {
e.printStackTrace();
}
}
// 当前时间
public String getDate() {
// TODO Auto-generated method stub
// 获得data对象
Date nowTime = new Date();
// 创建格式化参数
String pattern = "HH:mm:ss";
// 创建SimpleDateFormat对象
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
// 定义一个变量接收时间
String timePattern = sdf.format(nowTime);
// 返回当前时间
return timePattern;
}
}
// 通过实现 Runnable 接口创建线程
class runner2 implements Runnable {
Socket s;
public runner2(Socket s) {
this.s = s;
}
public void run() {
// String ip = s.getInetAddress().getHostAddress();
InputStream in = null;
try {
in = s.getInputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int len = 0;
byte[] buf = new byte[1024];
while (true) {
try {
len = in.read(buf);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String str1 = new String(buf, 0, len);// 服务器端传来的
if ("q".equals(str1)) {
new ServerScoket().quit=true;
break;// 结束聊天
}
System.out.println(str1);
}
}
}
2、
package socket;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
// 客户端
public class ClientScoket {
boolean quit=false;
private String ip = "127.0.0.1";// 设置成服务器IP
private int port = 6060;// 设置服务器端口号
static int i;
// 主函数
public static void main(String arg[]) throws InterruptedException {
// 1、客户端传文件或图片到服务器
String filepath=".\\Client\\Client.jpg";
new ClientScoket().ClientToServer(filepath);
// 2、服务器传文件或图片到客户端
new ClientScoket().ServerToClient();
// 3、服务器和客户端互聊
new ClientScoket().ServerSocketChat();
System.out.println("断开连接,聊天已结束!");
}
// 客户端传送文件或图片给服务器
public void ClientToServer(String filePath) {
try {
Socket socket = new Socket(ip, port);// 客户端socket
//while (true) {
// 选择进行传输的文件
File file = new File(filePath);
DataInputStream fin = new DataInputStream(
new BufferedInputStream(new FileInputStream(filePath)));
DataOutputStream fout = new DataOutputStream(
socket.getOutputStream());
// 将文件名传给服务器
fout.writeUTF(file.getName());
fout.flush();
System.out.println("传输文件大小:" + file.length() * 1.0 / 1024
+ " KB");
int bufferSize = 8 * 1024;
byte[] buf = new byte[bufferSize];
while (true) {
//System.out.print(file.getName() + "文件正在从客户端传给服务器...\n");
int read = 0;
if (fin != null) {
read = fin.read(buf);
}
if (read == -1) {
break;
}
fout.write(buf, 0, read);// 把buf中的数据写入服务器
}
fout.flush();
fin.close();
// 关闭socket
socket.close();
System.out.println(file.getName() + "文件已从客户端传给服务器,保存在Server文件夹中。");
//}
} catch (Exception e) {
e.printStackTrace();
}
}
// 从服务器端给客户端传文件
public void ServerToClient() {
try {
Socket socket = new Socket(ip, port+1);
//while (true) {
DataInputStream is = new DataInputStream(
socket.getInputStream());
OutputStream os = socket.getOutputStream();
// 得到文件名
String filename = ".\\Client\\";
filename += is.readUTF();
System.out.println("新生成的文件路径为:" + filename);
FileOutputStream fos = new FileOutputStream(filename);
byte[] b = new byte[1024];
int length = 0;
// 从socket输入流中读取数据
while ((length = is.read(b)) != -1) {
// 把socket输入流写到文件输出流中去
fos.write(b, 0, length);
}
fos.flush();
fos.close();
is.close();
socket.close();
//}
} catch (IOException e) {
e.printStackTrace();
}
}
// 服务器客户端互聊
public void ServerSocketChat() throws InterruptedException {
try {
Socket s = new Socket("localhost", port+2);
System.out.println("连接成功,可以开始愉快的聊天了!");
while (true) {
OutputStream out = s.getOutputStream();
Scanner scan = new Scanner(System.in);
String str = scan.next();
String s1 = getDate() + "\t客户端说:" + str;
System.out.println(s1);
out.write(s1.getBytes());
runner1 r = new runner1(s);// 代参构造函数构造
Thread t = new Thread(r);
t.start();
if(quit)
{
//out.write("q".getBytes());
return;
}
if ("q".equals(str)) {
out.write(str.getBytes());
t.sleep(1000);
return;// 结束聊天
}
}
//System.out.println("断开连接,聊天已结束!");
} catch (IOException e) {
e.printStackTrace();
}
}
// 当前时间
public String getDate() {
// TODO Auto-generated method stub
// 获得data对象
Date nowTime = new Date();
// 创建格式化参数
String pattern = "HH:mm:ss";
// 创建SimpleDateFormat对象
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
// 定义一个变量接收时间
String timePattern = sdf.format(nowTime);
// 返回当前时间
return timePattern;
}
}
// 通过实现 Runnable 接口创建线程
class runner1 implements Runnable {
Socket s;
public runner1(Socket s) {
this.s = s;
}
public void run() {
InputStream is = null;
try {
is = s.getInputStream();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
byte buf[] = new byte[1024];
int len = 0;
while (true) {
try {
len = is.read(buf);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String str1 = new String(buf, 0, len);// 服务器端传来的文字
if ("q".equals(str1)) {
new ClientScoket().quit=true;
break;// 结束聊天
}
System.out.println(str1);
}
}
}
标签:返回 ade 读取数据 print 服务器ip 获得 hat 服务器传文件 函数
原文地址:http://www.cnblogs.com/tianzijiaozi/p/7456436.html