1.socket网络编程

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()方法.

 

 

 

对船对

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值