java网络编程小结1

1 基本的tcp网络编程模式
   首先是server端,server端用的是serversocket,

 

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

public class TCPServer {
 public static void main(String[] args) throws Exception {
  ServerSocket ss = new ServerSocket(6666);
  while(true) {

       Socket s=ss.accept();

        DataInputStream dis=new DataInputStream(s.getInputStream());  

      System.out.println(dis.readUTF());
   dis.close();
   s.close();
  }
 }
}

 

   客户端
   

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

public class TCPClient {
 public static void main(String[] args) throws Exception {
  Socket s = new Socket("127.0.0.1", 6666);
  OutputStream os = s.getOutputStream();
  DataOutputStream dos = new DataOutputStream(os);
  Thread.sleep(30000);
  dos.writeUTF("hello server!");
  dos.flush();
  dos.close();
  s.close();
 }
}

  对客户端来说,是output输出流到服务端.

 

2 下面的程序,可以通过服务端把客户端的ip端口等输出给客户端
   

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

public class TestServer {
 public static void main(String args[]) {
  try {  
   ServerSocket s = new ServerSocket(8888);
   while (true) {
    Socket s1 = s.accept();
    OutputStream os = s1.getOutputStream();
    DataOutputStream dos = new DataOutputStream(os);
    dos.writeUTF("Hello," + s1.getInetAddress() +
      "port#" +s1.getPort() + "  bye-bye!");
    dos.close();
    s1.close();
   }
  }catch (IOException e) {
   e.printStackTrace();
   System.out.println("程序运行出错:" + e);   
  }
 }
}

 

客户端,则把服务端输进来的信息显示
  

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

public class TestClient {
 public static void main(String args[]) {
  try {
   Socket s1 = new Socket("127.0.0.1", 8888);
   InputStream is = s1.getInputStream();
   DataInputStream dis = new DataInputStream(is);
   System.out.println(dis.readUTF());
   dis.close();
   s1.close();
  } catch (ConnectException connExc) {
   connExc.printStackTrace();
   System.err.println("服务器连接失败!");
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
}

 

 

3 下面的TCP则是客户跟服务端各自发送一句话给对方.
  服务端:
   import java.io.*;
import java.net.*;
public class TestSockServer {
  public static void main(String[] args) {
    InputStream in = null;
    OutputStream out = null;
    try {
      ServerSocket ss = new ServerSocket(5888);
      Socket socket = ss.accept();
      in = socket.getInputStream();
      out = socket.getOutputStream();
      DataOutputStream dos = new DataOutputStream(out);
      DataInputStream dis = new DataInputStream(in);
      String s = null;
      if((s=dis.readUTF())!=null) {
       System.out.println(s);
       System.out.println("from: "+socket.getInetAddress());
       System.out.println("Port: "+socket.getPort());
     }
      dos.writeUTF("hi,hello");
      dis.close();
      dos.close();
      socket.close();
    } catch (IOException e) {e.printStackTrace();}
  }
}

   客户端:
   import java.net.*;
import java.io.*;
public class TestSockClient {
  public static void main(String[] args) {
    InputStream is = null; OutputStream os = null;
    try {
      Socket socket = new Socket("localhost",5888);
      is = socket.getInputStream();
      os = socket.getOutputStream();
      DataInputStream dis = new DataInputStream(is);
      DataOutputStream dos = new DataOutputStream(os);
      dos.writeUTF("hey");
      String s = null;
      if((s=dis.readUTF())!=null);
       System.out.println(s);
      dos.close();
      dis.close();
      socket.close();
    } catch (UnknownHostException e) {
       e.printStackTrace();
    } catch (IOException e) {e.printStackTrace();}
  }
}

5 UDP中,注意datagrampacket的应用,下面的是客户端发一消息给服务端的
   public class TestUDPClient
{
 public static void main(String args[]) throws Exception
 {
  byte[] buf = (new String("Hello")).getBytes();

  Datagrampacket dp=new DatagramPacket(buf, buf.length,
              new InetSocketAddress("127.0.0.1", 5678)
              );
  DatagramSocket ds = new DatagramSocket(9999);
  ds.send(dp);
  ds.close();
 }

  服务端:
   

import java.net.*;

public class TestUDPServer
{
 public static void main(String args[]) throws Exception
 {
  byte buf[] = new byte[1024];
  DatagramPacket dp = new DatagramPacket(buf, buf.length);
  DatagramSocket ds = new DatagramSocket(5678);
  while(true)
  {
   ds.receive(dp);
   System.out.println(new String(buf,0,dp.getLength()));
  }
 }
}

  

  要注意的知识点是,如果是客户端的其他类型的数据,在转换时要注意,比如下面的例子传一个长整型的到服务端:
客户端:
  public class TestUDPClient
{
 public static void main(String args[]) throws Exception
 {
  long n = 10000L;
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  DataOutputStream dos = new DataOutputStream(baos);
  dos.writeLong(n);
  
  byte[] buf = baos.toByteArray();
System.out.println(buf.length);
  
  DatagramPacket dp = new DatagramPacket(buf, buf.length,
              new InetSocketAddress("127.0.0.1", 5678)
              );
  DatagramSocket ds = new DatagramSocket(9999);
  ds.send(dp);
  ds.close();
  
 }
}

服务端:
  

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

public class TestUDPServer
{
 public static void main(String args[]) throws Exception
 {
  byte buf[] = new byte[1024];
  DatagramPacket dp = new DatagramPacket(buf, buf.length);
  DatagramSocket ds = new DatagramSocket(5678);
  while(true)
  {
   ds.receive(dp);
   ByteArrayInputStream bais = new ByteArrayInputStream(buf);
   DataInputStream dis = new DataInputStream(bais);
   System.out.println(dis.readLong());
  }
 }
}


   
   

你可能感兴趣的:(java网络编程)