基于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>

 

转载于:https://www.cnblogs.com/wlfhotte/archive/2011/12/06/2277861.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值