Socket基础学习

阅读更多

socket服务端需要以下几点:


Socket基础学习_第1张图片

SocketServer 代码:

 

package com.chen.study.socket.socketServer;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

import com.sun.jmx.snmp.tasks.ThreadService;

public class SocketServer {
	private static int port = 8000;
	private static String clientcount= "客戶端总數量:";
	private static String clientip= "当前连接客戶端IP:";
	
	/**
	 * @author Administrator
	 * socket服务端
	 */
	public static void SocketServer(){
		try {
			//1.创建SOCKET服务 给定 8000 端口
			ServerSocket socketserver = new ServerSocket(port);
			Socket socket = null;
			int count = 0;
			System.out.println("IP为:"+InetAddress.getLocalHost().getHostAddress()+"" +
					",端口为:"+port+" 的socket服务已经启动,等待客户端的请求.....");
			while(true){
			//2.启动监听,程序处于阻塞状态,等待客户点请求
			socket =  socketserver.accept(); 

                     //未发送完全的数据可以再等3600秒才关闭底层socket
                     socket.setSoLinger(true, 3600);   
			//采用多线程开启一个Socketserver
			ServerThread serverthread = new ServerThread(socket);
                        serverthread.setPriority(4); //设置优先级  0-5
			serverthread.start();
			count++;
			System.out.println(clientcount+count);
			InetAddress address = socket.getInetAddress();
			System.out.println(clientip+address.getHostAddress()+"\n----------------");
			
			
			if(count==3){
				socketserver.close();
				SocketServer();
			}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * 主函数启动Socket服务
	 */
	public static void main(String[] args) {
		SocketServer();
	}
}

 添加多线程,实现多客户端链接

 

ServerThread 代码:

 

package com.chen.study.socket.socketServer;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;

public class ServerThread extends Thread {
	//创建一个SOCKET类
	private static Socket socket = null;
	private static String severname = "我:";
	private static String clientname = "客户端说:";
	private static String seversay= "你好,服务端收到请求!";
	
	public ServerThread(Socket socket ){
		this.socket = socket;
	}
	//线程执行操作,重写父类RUN方法
	public void run(){

		//3.获取输入流,用来读取客户端发起的信息
		InputStream is  = null;
		BufferedReader br  = null;
		OutputStream os  = null;
		PrintWriter pw = null;
		try {
			is = socket.getInputStream();
		//将字节流转字符流,并给输入流添加缓冲
			br = new BufferedReader(new InputStreamReader(is));
		String info = null;
		while((info=br.readLine())!=null){
			System.out.println(clientname+info);
		}
		socket.shutdownInput();
		
		/*
		 * 回复客户端信息
		 */
		//获取输出流
		os = socket.getOutputStream();
		pw = new PrintWriter(os);
		System.out.println(severname+seversay);
		pw.write(seversay);
		pw.flush();
		socket.shutdownOutput();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				if(pw!=null){
					pw.close();
				}
				if(br!=null){
					br.close();
				}
				if(is!=null){
					is.close();
				}
				if(os!=null){
					os.close();
				}
				if(socket!=null){
					socket.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
	}
}

 

 

 

客户端
Socket基础学习_第2张图片

socket客户端代码:
 

package com.chen.study.socket.socketClient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;

public class SocketClient {
	private static int port = 8000;
	private static String ip = "192.168.119.35";
	private static String clientname = "我:";
	private static String severname = "服务器:";
	private static String clientsay = "你是服务端吗?";
	/**
	 * @param args
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		 socketClient();
	}
	
	/**
	 * socket客户端调用服务端
	 * @throws IOException 
	 */
	public static void socketClient() throws IOException{
		Socket socket = null;
		OutputStream os = null;
		PrintWriter pw = null;
		InputStream is = null;
		BufferedReader br = null;
		try {
			// 1.创建SOCkET客户端 ,配置制定的IP 端口
			socket = new Socket(ip, port);
			// 2.创建输出流
			os = socket.getOutputStream();
			pw = new PrintWriter(os);
			System.out.println(clientname+clientsay);
			pw.write(clientsay);
			pw.flush();
			socket.shutdownOutput(); //关闭输出流
			
			//3.获取服务端信息
			is = socket.getInputStream();
			br = new BufferedReader(new InputStreamReader(is));
			String info = null;
			while((info=br.readLine())!=null){
				System.out.println(severname+info);
			}
			socket.shutdownInput();//关闭输入流
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if(pw!=null){
				pw.close();
			}
			if(br!=null){
				br.close();
			}
			if(is!=null){
				is.close();
			}
			if(os!=null){
				os.close();
			}
			if(socket!=null){
				socket.close();
			}
		}
	}
}

 
 注意问题:

 

1、多线程 设置优先级

2、不需要关闭输入输出流

 

  • Socket基础学习_第3张图片
  • 大小: 191.7 KB
  • Socket基础学习_第4张图片
  • 大小: 142.1 KB
  • 查看图片附件

你可能感兴趣的:(scoket)