java的网络编程部分是如此有魅力,以至于一旦开始使用Java网络API,只要你能想到它就能够做得到.
可中断套接字实现的客户端
java网络编程可以实现功能完整的网络客户端和服务器,无论是希望编写特殊用途的Web服务器、安全的在线订单接收程序、简单的组播代理还是电子邮件客户端,都会找到可供学习和借用的代码。
下面我们来戳一下java网络编程,入个小门
1.HelloWorld 网络编程
package day20150810;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class SocketDemo {
public static void main(String[] args) {
Socket socket = null;
try {
//建立一个套接字
socket = new Socket("72.5.124.55", 13);
InputStream in = socket.getInputStream();
Scanner scanner = new Scanner(in);
while(scanner.hasNextLine()){
System.out.println(scanner.nextLine());
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
超时属性
socket.setSoTimeOut(1000);这个属性是建立连接后,如果规定时间内没有完成读写操作,就抛出SocketTimeOutException异常
获取主机地址
getAddress方法可以获取IP地址
package day20150810;
import java.net.InetAddress;
public class GetAddress {
public static void main(String[] args) throws Exception {
String host = "www.baidu.com";
InetAddress[] addresses = InetAddress.getAllByName(host);
for(InetAddress a:addresses)
System.out.println(a);
}
}
2.服务器实现
我们下面实现一个简单的聊天服务器
可以通过telnet进行通信
package day20150810;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
/**
*
* @author BigCaiCai
*
*/
public class ServerSocket {
public static void main(String[] args) throws Exception {
//我们使用8888这个端口
java.net.ServerSocket serverSocket = new java.net.ServerSocket(8888);
//建立一个负责检测8888这个端口的服务器
Socket socket = serverSocket.accept();
boolean isClose = false;
OutputStream outputStream = socket.getOutputStream();
InputStream inputStream = socket.getInputStream();
Scanner scanner = new Scanner(inputStream);
PrintWriter printWriter = new PrintWriter(outputStream,true);
printWriter.println("Enter Bye to close Server!");
while(!isClose&&scanner.hasNextLine()){
String get = scanner.nextLine();
if(get.equals("Bye")){
System.out.println("Bye~~");
printWriter.println("Bye~~");
}
else{
System.out.println(get.toString());
printWriter.println("Context:"+get);
}
}
System.out.println("结束");
inputStream.close();
outputStream.close();
}
}
3.聊天客户端简单实现
简单改一下helloworld后可以发现,有和telnet一样的效果了
package day20150810;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class SocketDemo {
public static void main(String[] args) {
Socket socket = null;
try {
//建立一个套接字
socket = new Socket("localhost", 8888);
boolean isClose = false;
InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
PrintWriter printWriter = new PrintWriter(out,true);
Scanner scanner = new Scanner(in);
printWriter.println("Hi,I'm c.");
while(!isClose&&scanner.hasNextLine()){
String get = scanner.nextLine();
if(get.equals("Bye")){
isClose=true;
}else{
Scanner w = new Scanner(System.in);
printWriter.println("c send Message:"+w.nextLine());
}
System.out.println();
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
4.利用多线程实现,可以为多个客户端服务的服务器
服务器代码
package day20150810;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
/**
*
* @author BigCaiCai
*
*/
public class ServerSocket {
public static void main(String[] args) throws Exception {
//我们使用8888这个端口
java.net.ServerSocket serverSocket = new java.net.ServerSocket(8888);
int i = 1;
Socket socket = null;
while(true){
//为每一个连接进来的服务器建立一个负责检测8888这个端口的服务器
socket = serverSocket.accept();
socket.setKeepAlive(true);
socket.setSoTimeout(100000);
OutputStream outputStream = socket.getOutputStream();
InputStream inputStream = socket.getInputStream();
Thread thread = new Thread(new SocketThread(outputStream, inputStream,i));
thread.start();
i++;
}
}
}
class SocketThread implements Runnable{
private OutputStream outputStream;
private InputStream inputStream;
private int i;
public SocketThread(OutputStream outputStream, InputStream inputStream,int i) {
super();
this.outputStream = outputStream;
this.inputStream = inputStream;
this.i = i ;
}
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("Clent"+i+"connect!");
boolean isClose = false;
Scanner scanner = new Scanner(inputStream);
PrintWriter printWriter = new PrintWriter(outputStream,true);
printWriter.println("Hi,I'm s.Enter Bye to close Server!");
while(!isClose&&scanner.hasNextLine()){
String get = scanner.nextLine();
if(get.equals("Bye")){
System.out.println("Bye");
printWriter.println("Bye~~");
isClose = true;
}
else{
System.out.println("Clent"+i+get.toString());
}
}
System.out.println("End connect "+i+"~~~");
try {
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
outputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
客户端代码
package day20150810;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class SocketDemo {
public static void main(String[] args) {
Socket socket = null;
try {
//建立一个套接字
socket = new Socket("localhost", 8888);
boolean isClose = false;
socket.setKeepAlive(true);
InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
PrintWriter printWriter = new PrintWriter(out,true);
Scanner scanner = new Scanner(in);
while(!isClose&&scanner.hasNextLine()){
String get = new Scanner(System.in).nextLine();
if(get.equals("Bye")){
isClose=true;
printWriter.println("c send Message:"+get);
}else{
printWriter.println("c send Message:"+get);
}
System.out.println();
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
可中断套接字实现的客户端
package day20150811;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.UnknownHostException;
import java.nio.channels.Channels;
import java.nio.channels.SocketChannel;
import java.util.Scanner;
public class SocketChannelClient {
public static void main(String[] args) {
SocketChannel socket = null;
try {
//建立一个套接字
socket = SocketChannel.open(new InetSocketAddress("localhost", 8888));
boolean isClose = false;
InputStream in = Channels.newInputStream(socket);
OutputStream out = Channels.newOutputStream(socket);
PrintWriter printWriter = new PrintWriter(out,true);
Scanner scanner = new Scanner(in);
while(!isClose&&scanner.hasNextLine()){
String get = new Scanner(System.in).nextLine();
if(get.equals("Bye")){
isClose=true;
printWriter.println("c send Message:"+get);
}else{
printWriter.println("c send Message:"+get);
}
System.out.println();
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}