socket通讯实例

服务端

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

//import com.sino.jft.frame.util.DateTimeUtils;

public class Server
{
      class Dispose extends Thread
      {
            //接受银联的socket
            Socket ssocket;
            /**返回银联的socket*/
            Socket returnUnionPaySocket;
            PrintWriter out;
            BufferedReader in;
            
            public Dispose (Socket s )
            {
                  this.ssocket = s;
            }

            public void run ()
            {
                  try
                  {
                        in = new BufferedReader(new InputStreamReader(ssocket.getInputStream()));
                        out = new PrintWriter(ssocket.getOutputStream(), true);
                      
                        String resMsg = in.readLine();
                        System.out.println("收到的报文是   " + resMsg);
                        out.println("报文受到");
                        try{
                              out.close();
                              in.close();
                              ssocket.close();
                        }
                        catch ( Exception e ){
                              e.printStackTrace();
                        }
                  }
                  catch ( Exception e ){
                        e.printStackTrace();
                  }
            }
      }

      public static void main ( String [] agrs )
      {
            try
            {
                  ServerSocket serverSocket = new ServerSocket(7002);
                  Server ser = new Server();
                  while ( true )
                  {
                        try
                        {
                              Socket ss = serverSocket.accept();
                              ser.new Dispose(ss).start();
                        }
                        catch ( Exception e )
                        {
                              e.printStackTrace();
                        }
                  }
            }
            catch ( Exception e )
            {
                  e.printStackTrace();
            }
      }
}


客户端

import java.io.*;
import java.net.*;

public class ClientTest
{
      Socket socket;
      BufferedReader in;
      PrintWriter out;

      public ClientTest ()
      {
            try
            {
                  socket = new Socket("127.0.0.1", 7002);

                  out = new PrintWriter(socket.getOutputStream(), true);
                  in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

                  out.print("FEBFE0000000018001无锡        2009102520091026                        RATING_5                                                                        000000");
                  out.println("2次请求");
                  char [] cbuf = new char [1];
                  StringBuffer sb = new StringBuffer();
                  while ( true )
                  {
                        int read = 0;
                        if ( in != null )
                        {
                              read = in.read(cbuf);
                              sb.append(cbuf);
                        }
                        if ( read == -1 )
                        {
                              break;
                        }
                  }
                  String localDir = "D:/hotelTest";
                  File tmpDir = new File(localDir);
                  if ( !tmpDir.isDirectory() )
                  {
                        tmpDir.mkdirs();
                  }
                  String fullFiles = localDir + "/" + "test.txt";
                  fullFiles = fullFiles.replaceAll("//", "/");
                  try
                  {
                        FileWriter fwmx = new FileWriter(fullFiles);
                        fwmx.write(cbuf);
                        fwmx.flush();
                        fwmx.close();
                  }
                  catch ( Exception e )
                  {
                        e.printStackTrace();
                  }
                  out.close();
                  in.close();
                  socket.close();
            }
            catch ( IOException e )
            {
            }
      }

      public static void main ( String [] args )
      {
            new ClientTest();
      }
}

你可能感兴趣的:(java,thread,.net,socket)