62,614
社区成员
发帖
与我相关
我的任务
分享
* @param args
*/
public static void main(String[] args) {
Socket server;
try {
server = new Socket(InetAddress.getByName("127.0.0.1"), 8888);
server.setKeepAlive(true);
//获取输入输出流
PrintStream ps = new PrintStream(server.getOutputStream(), false, "UTF-8");
//BufferedReader in = new BufferedReader(new InputStreamReader(server.getInputStream()));
int i = 0;
while(true) {
System.out.println("server.isConnected()=" + server.isConnected());
System.out.println("server.isOutputShutdown()=" + server.isOutputShutdown());
System.out.println("hello" + i);
ps.println("hello" + i); //自动在末尾加入分隔符
ps.flush();
i++;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.BindException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
*
* @author Administrator
* 使用TCP协议的服务器Demo
*/
public class SdServer {
boolean started = false; //服务器是否启动
ServerSocket ss = null; //服务器对象
private static int port = 8888; //服务器监听的端口
private static final String LS = System.getProperty("line.separator");
public static void main(String[] args) {
//根据传入的参数动态改变服务器的监听端口
if(args.length != 0) {
port = Integer.parseInt(args[0]);
}
System.out.println("Service begins...");
new SdServer().start();
}
public void start() {
try {
ss = new ServerSocket(port);
started = true;
} catch (BindException e) {
System.out.println("The port has been used...");
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
}
try {
while(started) {
Socket s = ss.accept(); //创建阻塞模式的服务器
Client c = new Client(s);
System.out.println("a client connected!");
new Thread(c).start();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
*
* @author Administrator
* 客户端类
*/
class Client implements Runnable {
private Socket s;
private DataInputStream dis = null; //获取数据输入流
private DataOutputStream dos = null; //获取数据
BufferedReader in = null; //获取缓存流的Reader
BufferedWriter out = null; //获取缓存流的Writer
private boolean bConnected = false; //客户端是否正常连接
private boolean isFileWriting = false; //是否读写文件
public Client(Socket s) {
this.s = s;
try {
dis = new DataInputStream(s.getInputStream());
in = new BufferedReader(new InputStreamReader(dis,"utf-8"));
dos = new DataOutputStream(s.getOutputStream());
out = new BufferedWriter(new OutputStreamWriter(dos));
bConnected = true;
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
//获取刘异常
if(!bConnected) {
return;
}
try {
System.out.println("*****************************************************");
DateFormat df=new SimpleDateFormat("yyyy-MM-dd EE hh:mm:ss");
//打印连接时间及客户端信息
System.out.println("Time:" + df.format(new Date()));
System.out.println("Client IP:" + s.getLocalAddress().getHostAddress());
//开始输出获取的信息
System.out.println("Receive Msg:");
String str = "";
int i = 1;
File file = new File(i + ".txt");
while(str != null) {
//客户端异常关闭,则这里会抛出一个IOException异常
str = in.readLine();
//str = new String(str.getBytes("UTF-8"),"UTF-8");
System.out.println("消息:" + str);
//是否写入文件
if(isFileWriting) {
boolean flag = false;
if(!file.exists()) { //不存在则创建文件
flag = file.createNewFile();
} else {
flag = true;
}
if(flag) {
FileOutputStream fos = new FileOutputStream(file,true);
fos.write(str.getBytes("UTF-8"));
fos.flush();
fos.close();
}
}
out.write("copy your word" + LS);
out.flush();
}
System.out.println("Connection over");
} catch(SocketException e){
System.out.println("Client closed the socket exception!");
bConnected = false;
//e.printStackTrace();
//System.exit(1);
}catch (EOFException e) {
System.out.println("End of file exception!");
bConnected = false;
} catch (IOException e) {
System.out.println("IO Exception!");
e.printStackTrace();
bConnected = false;
} finally {
try {
//简单释放下资源,其他流的资源在此处不做释放
if(in != null) in.close();
if(out != null) out.close();
if(s != null) s.close();
} catch (IOException e1) {
e1.printStackTrace();
System.out.println("Thread closed Exception.");
}
}
System.out.println("*****************************************************");
}
}
}
public static void main(String[] args) {
Socket server;
try {
server = new Socket("127.0.0.1", 8888);
server.setKeepAlive(true);
//获取输入输出流
PrintStream ps = new PrintStream(server.getOutputStream(), false, "UTF-8");
//BufferedReader in = new BufferedReader(new InputStreamReader(server.getInputStream()));
int i = 0;
while(true) {
try{
server.sendUrgentData(1);
}catch(IOException e){
System.out.println("Server Down");
}
System.out.println("server.isConnected()=" + server.isConnected());
System.out.println("server.isOutputShutdown()=" + server.isOutputShutdown());
System.out.println("hello" + i);
ps.println("hello" + i); //自动在末尾加入分隔符
ps.flush();
i++;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}