nettalkclient.java

来自「java编写的聊天室」· Java 代码 · 共 67 行

JAVA
67
字号
package com.java.NetTalk;


import java.io.IOException;
import java.net.Socket;
import java.net.InetAddress;
import java.util.Scanner;
import com.java.*;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;



public class NetTalkClient extends MyNet{
		public NetTalkClient(){
			
		}
		
		public NetTalkClient(String sHost,int port)throws IOException,ProtNetException{
			if(port < NetTalkClient.SYSTEM_PROT_MAXNUMBER)
				throw new ProtNetException("Prot error");
			socket = new Socket(sHost,port);
			socketChannel = socket.getChannel();
		}
		
		public void  SetContent(String sHost,int port)throws IOException,ProtNetException{
				if(port > NetTalkClient.SYSTEM_PROT_MAXNUMBER)
					throw new ProtNetException("Prot error");
				socket = new Socket(sHost,port);
				socketChannel = socket.getChannel();
		} 
		
		public NetTalkClient(InetAddress address, int port)throws IOException,ProtNetException{
			if(port > NetTalkClient.SYSTEM_PROT_MAXNUMBER)
				throw new ProtNetException("Prot error");
			socket = new Socket(address,port);
		}
		
		public void DisplayInfo(){
			
		}
		
		public int GetPort(){
				return socket.getPort();
		}
		
		public InetAddress GetConnectionAddress(){
				return socket.getInetAddress();
		}
		
		public boolean SendMessage(ByteBuffer message) throws IOException,NotOpenNetException{
				if(socketChannel.isOpen()){
					throw new NotOpenNetException("Not Open Channel");
				}
				socketChannel.write(message);
				return true;
		}
		
		public void CloseSocket() throws IOException{
				socket.close();
		} 
	
	private static final int SYSTEM_PROT_MAXNUMBER = 1024;
	private SocketChannel  socketChannel;
	private Socket socket;
}

⌨️ 快捷键说明

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