简单的TCP客户端
import java.net.*;
import java.io.*;
public class SimpleTcpClient {
public static void main(String[] args) throws IOException
{
Socket socket = null;
BufferedReader br = null;
PrintWriter pw = null;
BufferedReader brTemp = null;
try
{
socket = new Socket(InetAddress.getLocalHost(), 5678);
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
pw = new PrintWriter(socket.getOutputStream());
brTemp = new BufferedReader(new InputStreamReader(System.in));
while(true)
{
String line = brTemp.readLine();
pw.println(line);
pw.flush();
if (line.equals("end")) break;
System.out.println(br.readLine());
}
}
catch(Exception ex)
{
System.err.println(ex.getMessage());
}
finally
{
if (socket != null) socket.close();
if (br != null) br.close();
if (brTemp != null) brTemp.close();
if (pw != null) pw.close();
}
}
}
简单版本TCP服务器端
import java.net.*;
import java.io.*;
public class SimpleTcpServer {
public static void main(String[] args) throws IOException
{
ServerSocket server = null;
Socket client = null;
BufferedReader br = null;
PrintWriter pw = null;
try
{
server = new ServerSocket(5678);
client = server.accept();
br = new BufferedReader(new InputStreamReader(client.getInputStream()));
pw = new PrintWriter(client.getOutputStream());
while(true)
{
String line = br.readLine();
pw.println("Response:" + line);
pw.flush();
if (line.equals("end")) break;
}
}
catch(Exception ex)
{
System.err.println(ex.getMessage());
}
finally
{
if (server != null) server.close();
if (client != null) client.close();
if (br != null) br.close();
if (pw != null) pw.close();
}
}
}
多线程版本的TCP服务器端
import java.net.*;
import java.io.*;
public class SmartTcpServer {
public static void main(String[] args) throws IOException
{
ServerSocket server = new ServerSocket(5678);
while(true)
{
Socket client = server.accept();
Thread thread = new ServerThread(client);
thread.start();
}
}
}
class ServerThread extends Thread
{
private Socket socket = null;
public ServerThread(Socket socket)
{
this.socket = socket;
}
public void run() {
BufferedReader br = null;
PrintWriter pw = null;
try
{
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
pw = new PrintWriter(socket.getOutputStream());
while(true)
{
String line = br.readLine();
pw.println("Response:" + line);
pw.flush();
if (line.equals("end")) break;
}
}
catch(Exception ex)
{
System.err.println(ex.getMessage());
}
finally
{
if (socket != null)
try {
socket.close();
} catch (IOException e1) {
e1.printStackTrace();
}
if (br != null)
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
if (pw != null) pw.close();
}
}
}
一个简单的TCP连接池
import java.net.*;
import java.io.*;
public class TcpConnectionPool {
private InetAddress address = null;
private int port;
private Socket[] arrSockets = null;
private boolean[] arrStatus = null;
private int count;
public TcpConnectionPool(InetAddress address, int port, int count)
{
this.address = address;
this.port = port;
this .count = count;
arrSockets = new Socket[count];
arrStatus = new boolean[count];
init();
}
private void init()
{
try
{
for (int i = 0; i < count; i++)
{
arrSockets[i] = new Socket(address.getHostAddress(), port);
arrStatus[i] = false;
}
}
catch(Exception ex)
{
System.err.println(ex.getMessage());
}
}
public Socket getConnection()
{
if (arrSockets == null) init();
int i = 0;
for(i = 0; i < count; i++)
{
if (arrStatus[i] == false)
{
arrStatus[i] = true;
break;
}
}
if (i == count) throw new RuntimeException("have no connection availiable for now.");
return arrSockets[i];
}
public void releaseConnection(Socket socket)
{
if (arrSockets == null) init();
for (int i = 0; i < count; i++)
{
if (arrSockets[i] == socket)
{
arrStatus[i] = false;
break;
}
}
}
public void reBuild()
{
init();
}
public void destory()
{
if (arrSockets == null) return;
for(int i = 0; i < count; i++)
{
try
{
arrSockets[i].close();
}
catch(Exception ex)
{
System.err.println(ex.getMessage());
continue;
}
}
}
}
UDP通信客户端
import java.net.*;
import java.io.*;
public class UdpClient {
public static void main(String[] args)
{
try
{
InetAddress host = InetAddress.getLocalHost();
int port = 5678;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while(true)
{
String line = br.readLine();
byte[] message = line.getBytes();
DatagramPacket packet = new DatagramPacket(message, message.length, host, port);
DatagramSocket socket = new DatagramSocket();
socket.send(packet);
socket.close();
if (line.equals("end")) break;
}
br.close();
}
catch(Exception ex)
{
System.err.println(ex.getMessage());
}
}
}
UDP通信服务器端
import java.net.*;
import java.io.*;
public class UdpServer {
public static void main(String[] args)
{
try
{
int port = 5678;
DatagramSocket dsSocket = new DatagramSocket(port);
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
while(true)
{
dsSocket.receive(packet);
String message = new String(buffer, 0, packet.getLength());
System.out.println(packet.getAddress().getHostName() + ":" + message);
if (message.equals("end")) break;
packet.setLength(buffer.length);
}
dsSocket.close();
}
catch(Exception ex)
{
System.err.println(ex.getMessage());
}
}
}
多播通信客户端
import java.net.*;
import java.io.*;
public class MulticastClient {
public static void main(String[] args)
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try
{
InetAddress address = InetAddress.getByName("230.0.0.1");
int port = 5678;
while(true)
{
String line = br.readLine();
byte[] message = line.getBytes();
DatagramPacket packet = new DatagramPacket(message, message.length, address, port);
MulticastSocket multicastSocket = new MulticastSocket();
multicastSocket.send(packet);
if (line.equals("end")) break;
}
br.close();
}
catch(Exception ex)
{
System.err.println(ex.getMessage());
}
}
}
多播通信服务器端
import java.net.*;
import java.io.*;
public class MulticastServer {
public static void main(String[] args)
{
int port = 5678;
try
{
MulticastSocket multicastSocket = new MulticastSocket(port);
InetAddress address = InetAddress.getByName("230.0.0.1");
multicastSocket.joinGroup(address);
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
while(true)
{
multicastSocket.receive(packet);
String message = new String(buffer, packet.getLength());
System.out.println(packet.getAddress().getHostName() + ":" + message);
if (message.equals("end")) break;
packet.setLength(buffer.length);
}
multicastSocket.close();
}
catch(Exception ex)
{
System.err.println(ex.getMessage());
}
}
}
NIO例子
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.nio.charset.*;
import java.net.*;
public class NewIOSample {
public static void main(String[] args)
{
String host="127.0.0.1";
int port = 5678;
SocketChannel channel = null;
try
{
InetSocketAddress address = new InetSocketAddress(host,port);
Charset charset = Charset.forName("UTF-8");
CharsetDecoder decoder = charset.newDecoder();
CharsetEncoder encoder = charset.newEncoder();
ByteBuffer buffer = ByteBuffer.allocate(1024);
CharBuffer charBuffer = CharBuffer.allocate(1024);
channel = SocketChannel.open();
channel.connect(address);
String request = "GET / \r\n\r\n";
channel.write(encoder.encode(CharBuffer.wrap(request)));
while((channel.read(buffer)) != -1)
{
buffer.flip();
decoder.decode(buffer, charBuffer, false);
charBuffer.flip();
System.out.println(charBuffer);
buffer.clear();
charBuffer.clear();
}
}
catch(Exception ex)
{
System.err.println(ex.getMessage());
}
finally
{
if (channel != null)
try {
channel.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有