package com.pd;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import org.apache.jasper.tagplugins.jstl.core.Out;
import org.springframework.stereotype.Service;
import com.mysql.fabric.Server;
public class sever1 {
public void start(){
new Thread(){
public void run() {
try {
//在8000端口上启动服务,针对访问8000的端口进行一个监听的操作.
ServerSocket ss = new ServerSocket(8000);
System.out.println("服务器已启动");
//循环等待客户端连接
while(true) {//建立一个死循环的操作,
Socket s = ss.accept(); //通过ss这个对象去获取访问8000端口的ip然后通过传递引用的过程.
//启动通信线程,对连接通道s执行通信过程
tongxinThread t=new tongxinThread(s);
t.start();
ss.close();//accept()进行一个关闭,因为不停地对端口进行一个死循环,容易引发以下问题
//java.net.BindException: Address already in use: JVM_Bind
}
} catch (Exception e) {
System.out.println( "服务无法在8000端口上启动,或者服务已停止");
e.printStackTrace();
}
}
}.start();//start()方法用于去启动线程
}
public static void main(String[] args) throws IOException {
sever1 s=new sever1();//使用默认的构造方法进行实例的一个创造
s.start();//实例化之后去调用对象中的其中一个方法.
/**
* 使用的是Thread.start()线程的方法去进行一个线程的构造方法的使用.
*/
}
}
public class tongxinThread extends Thread {//继承线程的接口然后进行一个操作
Socket s;
public tongxinThread(Socket s2) {
this.s=s;
}
public void run(){
try {
//输入流
BufferedReader in=new BufferedReader(new InputStreamReader(s.getInputStream(), "UTF-8"));
//输出流
BufferedWriter out=new BufferedWriter(new OutputStreamWriter(s.getOutputStream(),"UTF_8"));
String line;//定义一个行进行对流数据的输入进行一个设定操作
while((line=in.readLine())!=null){//每一行读入的数据不为空
System.out.println(line);
out.flush();//进行一个输出流对象的一个刷新操作.
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
运行结果如下所示:
服务器已启动
其中相关的源码剖析结果如下所示:
public
class ServerSocket implements java.io.Closeable {
/**
* Various states of this socket.
*/
private boolean created = false;
private boolean bound = false;
private boolean closed = false;
private Object closeLock = new Object();
/**
* The implementation of this Socket.
*/
private SocketImpl impl;
对传入端口的的一个监听,源码剖析如下所示:
public ServerSocket(int port) throws IOException {
this(port, 50, null);
}
//服务端接收客户端传递过来的源码剖析结果如下所示:
public Socket accept() throws IOException {
if (isClosed())
throw new SocketException("Socket is closed");
if (!isBound())
throw new SocketException("Socket is not bound yet");
//以下进行一个socket的通信的建立,以及一个implAccept()方法的实现
Socket s = new Socket((SocketImpl) null);
implAccept(s);
return s;
}
implAccept(s)方法的具体实现如下所示:
protected final void implAccept(Socket s) throws IOException {
SocketImpl si = null;
try {
if (s.impl == null)
s.setImpl();
else {
s.impl.reset();
}
si = s.impl;
s.impl = null;
si.address = new InetAddress();
si.fd = new FileDescriptor();
getImpl().accept(si);
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkAccept(si.getInetAddress().getHostAddress(),
si.getPort());
}
} catch (IOException e) {
if (si != null)
si.reset();
s.impl = si;
throw e;
} catch (SecurityException e) {
if (si != null)
si.reset();
s.impl = si;
throw e;
}
s.impl = si;
s.postAccept();
}
线程启动方法的剖析,相关剖析结果如下所示:
public synchronized void start() {
/**
* This method is not invoked for the main method thread or "system"
* group threads created/set up by the VM. Any new functionality added
* to this method in the future may have to also be added to the VM.
*
* A zero status value corresponds to state "NEW".
*/
if (threadStatus != 0)
throw new IllegalThreadStateException();
/* Notify the group that this thread is about to be started
* so that it can be added to the group's list of threads
* and the group's unstarted count can be decremented. */
group.add(this);
boolean started = false;
try {
start0();
started = true;
} finally {
try {
if (!started) {
group.threadStartFailed(this);
}
} catch (Throwable ignore) {
/* do nothing. If start0 threw a Throwable then
it will be passed up the call stack */
}
}
}
通过相关的线程组去启动相关的线程的一些操作.根据Sync的线程锁去保证单个线程启动的安全性.
总结几个要素,thread.start()方法.
对船对