⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 service.java

📁 scoket 网络聊天室支持私聊
💻 JAVA
字号:
package chenmin.server;

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;
//import java.util.*;
/**
 * 类名:用户服务类
 * 作用:接收发送用户信息
 * 文件名:Service.java
 * 最后修改日期 20060917
 * @author Chenmin
 */
public class Service implements Runnable
{
	private Socket s;
	private boolean fresh=false;
	private String word;
	private String username;
	private String who;
	boolean running=false;
	int id;
	public boolean isRunning()
	{
		return running;
	}
	public synchronized void setRunning(boolean flag)
	{
		running=flag;
		if(flag==true)
		{
			this.notify();
		}
	}
	public Service(int _id)
	{
		id=_id;
		running=false;
	}
	public void setSocket(Socket _s)
	{
		s=_s;
	}
	public Socket getSocket()
	{
		return s;
	}
	public Service(Socket _s)
	{
		s=_s;
	}
	public String getWord()
	{
		return word;
	}
	public void setWord(String _word)
	{
		word=_word;
	}
	public String getWho()
	{
		return who;
	}
	public void setWho(String _who)
	{
		who=_who;
	}
	public boolean getFresh()
	{
		return fresh;
	}
	public void setFresh(boolean _fresh)
	{
		fresh=_fresh;
	}
	public String getUsername()
	{
		return username;
	}
	public void setUsername(String _username)
	{
		username=_username;
	}
	public void sendWord(String _word)
	{
		//InputStream ips;
		try {
			//ips = s.getInputStream();
			OutputStream ops=s.getOutputStream();

			//为输入流字符化,并套上一个缓冲流
		//	BufferedReader br = new BufferedReader(new InputStreamReader(ips));
			//将输出流包上一个字符流。
			PrintWriter pw = new PrintWriter(ops);
			pw.println(_word);				
			pw.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	public synchronized void run()
	{
		while(true)
		{
			if (running==false)
			{
				try {
					System.out.println("Thread "+id+" wait!");
					this.wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			try
			{
				InputStream ips=s.getInputStream();
				//OutputStream ops=s.getOutputStream();
				//为输入流字符化,并套上一个缓冲流
				BufferedReader br = new BufferedReader(new InputStreamReader(ips));
				//将输出流包上一个字符流。
				//PrintWriter pw = new PrintWriter(ops);
				while(true)
				{
					//从socket中读入流
					who=br.readLine();
					String wordbuf=new String("");
					//Byte b[] =new Byte[1024];
					int c=0;
					/*wordbuf ="";
				
					while ((c=br.read())!=-1)
					{
						wordbuf=wordbuf+(char)c;
						System.out.println("Thread "+id+"  :"+wordbuf);
					}
					word=wordbuf;*/
					word = br.readLine();
					//word = br.read();
					//System.out.println("Thread "+id+"  :"+wordbuf);
					fresh=true;				
				}
				//br.close();
				//关闭包装类,会自动关闭包装类中所包装的底层类。所以不用调用ips.close()
				//pw.close();
				//s.close();
			}
			catch(Exception e)
			{
				//e.printStackTrace();
				System.out.println("Disconnect with " +
						"Socket:"+s.getInetAddress()+":"+s.getPort());
				try {
					this.setRunning(false);
					System.out.println("Thread "+id+" wait!");
					this.wait();
				} catch (InterruptedException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		}
		
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -