基于ant的socket编程
服务器端
package org.zqm.socket;
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.ServerSocket;
import java.net.Socket;
public class TcpServer {
private ServerSocket server;
private Socket connection;
public TcpServer() {
}
//修饰输出流
public PrintWriter getOutStream(Socket connection) throws IOException{
OutputStream out = connection.getOutputStream();
PrintWriter print = new PrintWriter(out, true);
return print;
}
//修饰输入流
public BufferedReader getInputStream(Socket connection) throws IOException{
InputStream in = connection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
return br;
}
public void run(){
try {
server = new ServerSocket(8081);//绑定ip(默认本地一个可用ip),端口
System.out.println("=== 开始监听 ===");
connection = server.accept();//进入LISTEN状态,若返回成功socket对象则表明tcp三次握手成功,连接建立
System.out.println("=== tcp三次握手成功 ===");
PrintWriter writer = getOutStream(connection);
BufferedReader reader = getInputStream(connection);
String data = null;
while ((data = reader.readLine()) != null){
System.out.println("服务器读取数据:" + data);
writer.println("server response:" + data);
if ("bye".equals(data)){
System.out.println("=== 停止服务器 ===");
break;
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if (connection != null){
try {
connection.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void main(String [] args) throws IOException{
TcpServer s = new TcpServer();
s.run();
}
}
客户端
package org.zqm.socket;
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.net.UnknownHostException;
public class TcpClient {
private Socket client;
public TcpClient () {
}
//修饰输出流
public PrintWriter getOutStream(Socket connection) throws IOException{
OutputStream out = connection.getOutputStream();
PrintWriter print = new PrintWriter(out, true);
return print;
}
//修饰输入流
public BufferedReader getInputStream(Socket connection) throws IOException{
InputStream in = connection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
return br;
}
public void run(){
try {
//若构成socket对象成功,则表示tcp三次握手成功。
System.out.println("=== client 开始tcp三次握手 ===");
client = new Socket("localhost",8081);
System.out.println("=== client tcp三次握手成功 ===");
PrintWriter writer = getOutStream(client);
BufferedReader reader = getInputStream(client);
BufferedReader controlerReader = new BufferedReader(new InputStreamReader(System.in));
String data ;
while((data = controlerReader.readLine()) != null){
writer.println(data);
System.out.println("=== 客户端写入:" + data);
String response = reader.readLine();
System.out.println("=== 服务的回复:" + response);
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if (client != null){
try {
client.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void main(String [] args) throws UnknownHostException, IOException{
TcpClient c = new TcpClient();
c.run();
}
}
build.xml
<?xml version="1.0" ?>
<project name="zqmant1" default="deploy">
<target name="init">
<mkdir dir="build/classes" />
<mkdir dir="dist" />
</target>
<target name="compile" depends="init">
<javac srcdir="src" destdir="build/classes" />
</target>
<target name="doc" depends="init">
<javadoc destdir="build/classes" sourcepath="src"/>
</target>
<target name="deploy" depends="compile,doc">
<jar destfile="dist/porject.jar" basedir="build/classes"/>
</target>
</project>