java小试牛刀之tcp-udp连接

     最近做项目需要在web应用中,与短信网关建立一个长连接,想到一个问题,如果web应用进程死掉了,那么这个进程所起的TCP连接,会如何?

     这个问题,应该是TCP/IP应用中一个基本的问题,经测试进程关闭后,进程所发起的连接均不存在了。有待进一步跟踪。

 

TCP test class

 

单线程server

package com.umpay.bank.test.ywr;

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

public class MyServer {

 public static void main(String[] args) throws IOException {
  ServerSocket server = new ServerSocket(5678);
  Socket client = server.accept();
  BufferedReader in = new BufferedReader(new InputStreamReader(client
    .getInputStream()));
  PrintWriter out = new PrintWriter(client.getOutputStream());
  while (true) {
   String str = in.readLine();
   System.out.println(str);
   out.println("has receive....");
   out.flush();
   if (str.equals("end"))
    break;
  }
  client.close();
 }
}

 

//多线程server

package com.umpay.bank.test.ywr;

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

public class MultiUser extends Thread {
 private Socket client;

 public MultiUser(Socket c) {
  this.client = c;
 }

 public void run() {
  try {
   BufferedReader in = new BufferedReader(new InputStreamReader(client
     .getInputStream()));
   PrintWriter out = new PrintWriter(client.getOutputStream());
   // Mutil User but can't parallel
   while (true) {
    String str = in.readLine();
    System.out.println(str);
    out.println("has receive....");
    out.flush();
    if (str.equals("end"))
     break;
   }
   client.close();
  } catch (IOException ex) {
  } finally {

  }
 }

 public static void main(String[] args) throws IOException {
  ServerSocket server = new ServerSocket(5678);
  while (true) {
   // transfer location change Single User or Multi User
   MultiUser mu = new MultiUser(server.accept());
   mu.start();
  }
 }
}

//客户端

package com.umpay.bank.test.ywr;

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

public class MyClient {
 static Socket server;

 public static void main(String[] args) throws Exception {
  server = new Socket(InetAddress.getLocalHost(), 5678);
  BufferedReader in = new BufferedReader(new InputStreamReader(server
    .getInputStream()));
  PrintWriter out = new PrintWriter(server.getOutputStream());
  BufferedReader wt = new BufferedReader(new InputStreamReader(System.in));

  while (true) {
   String str = wt.readLine();
   out.println(str);
   out.flush();
   if (str.equals("end")) {
    break;
   }
   System.out.println(in.readLine());
  }
  server.close();
 }
}

 

UDP test class

 

//接收端--需要在发送端发送之前启动

package com.umpay.bank.test.ywr;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class UDPReceiver {
 public static void main(String[] args) throws Exception {
  DatagramSocket ds = new DatagramSocket(3000);
  byte[] buf = new byte[1024];
  DatagramPacket dp = new DatagramPacket(buf, 1024);
  ds.receive(dp);
  System.out.println(new String(dp.getData(), 0, dp.getLength()));
  System.out.println(dp.getAddress().getHostAddress());
  System.out.println(dp.getPort());
  ds.close();
 }

 

//发送端

package com.umpay.bank.test.ywr;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class UDPSender {
 public static void main(String[] args) throws Exception {
  DatagramSocket ds = new DatagramSocket();
  String str = "代码来源于网络,贡献于网络";
  ds.send(new DatagramPacket(str.getBytes(), str.getBytes().length,
    InetAddress.getByName("10.10.36.89"), 3000));
  ds.close();
 }
}

 


}

 

你可能感兴趣的:(java,Web,.net,socket,网络应用)