您的位置:首页 > 理论基础 > 计算机网络

21---网络编程(补充)

2012-03-14 15:50 162 查看
java.net包中定义的两个类Socket和ServerSocket,分别用来实现双向连接到client和server;

TCP端口和UDP端口是分开的,每个都有65536个;

首先启动server在启动client---这时应该启动两个dos窗口,一个编译执行ServerSocket一个编译执行Socket

====================服务器端======================

import java.net.*;

public class TCPServer{

public static void main(String args[])throws Exception{

//相当于起了个服务,并且监听的是6666---即该服务占得事6666这个端口

ServerSocket ss=new ServerSocket(6666);

/**

*对于服务器端的该服务,可能有n个客户端去连接,所以服务器应该不断的去接

*收(ss.accept())----服务器端一般写个死循环,这样可以不断的接受客户端的

*请求;服务器端接受一个请求,就创建一个Socket,用这个Socket来与客户端

*进行交互;

*/

while(true){//死循环

//接受客户端的连接,accept是堵塞的,没有连接就等待

Socket s=ss.accept();

//有连接上面就执行,下面就跟着执行,所以回输出下面的内容

System.out.println("A client connect!");

}

}

}

====================客户端======================

import java.net.*;

public class TCPClient{

public static void main(String args[]) throws Exception{

//创建到指定ip和端口的连接,客户端的这个服务也占端口的

//该端口系统随机分配

Socket s=new Socket("127.0.0.1",6666);

}

}

=========================================================================================================

import java.net.*;

import java.io.*;

public class TCPServer{

public static void main(String args[])throws Exception{

//相当于起了个服务,并且监听的是6666---即该服务占得事6666这个端口

ServerSocket ss=new ServerSocket(6666);

/**

*对于服务器端的该服务,可能有n个客户端去连接,所以服务器应该不断的去接

*收(ss.accept())----服务器端一般写个死循环,这样可以不断的接受客户端的

*请求;服务器端接受一个请求,就创建一个Socket,用这个Socket来与客户端

*进行交互;

*/

while(true){//死循环

//接受客户端的连接,accept是堵塞的,没有连接就等待

Socket s=ss.accept();

//相当于创建一个来自网络连接的输入流,并被处理流包装一下

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

//readUTF也是堵塞式的,如果没写东西,则会等待,知道写了为止

System.out.print(dis.readUTF());

dis.close();

s.close();

}

}

}

------------------------------------------------------------------------------------------------------

import java.net.*;

import java.io.*;

public class TCPClient{

public static void main(String args[]) throws Exception{

//创建到指定ip和端口的连接,客户端的这个服务也占端口的

//该端口系统随机分配

Socket s=new Socket("127.0.0.1",6666);

//相当于创建一个指向TCP连接的输出流

OutputStream os=s.getOutputStream();

//包装一下字节流

DataOutputStream dos=new DataOutputStream(os);

dos.writeUTF("hello server!");

dos.flush();

dos.close();

s.close();

}

}

*****************************************************服务器与客户端的交互**************************************

import java.net.*;

import java.io.*;

public class TCPServer{

public static void main(String args[])throws Exception{

ServerSocket ss=new ServerSocket(6666);

InputStream in=null;

OutputStream out=null;

try{

Socket s=ss.accept();

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

DataOutputStream dos=new DataOutputStream(s.getOutputStream());

String str=null;

if((str=dis.readUTF())!=null){

System.out.println(str);

System.out.println("From:"+s.getInetAddress());

System.out.println("Port:"+s.getPort());

}

dos.writeUTF("hi,client!");

dis.close();

dos.close();

s.close();

}catch(IOException e){e.printStackTrace();}

}

}

---------------------------------------------------------------------------------------------------------

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();

InputStream in=s.getInputStream();

DataOutputStream dos=new DataOutputStream(os);

DataInputStream dis=new DataInputStream(in);

dos.writeUTF("hello server!");

System.out.println(dis.readUTF());

dos.flush();

dos.close();

dis.close();

s.close();

}

}

===========================================================================================

import java.net.*;

public class UDPServer{

public static void main(String args[]) throws Exception{

byte buf[]=new byte[1024];

//创建数据包,而数据包中真正存数据的是上面的数组

DatagramPacket dp=new DatagramPacket(buf,buf.length);

//创建一个DatagramSocket(数据包插座)---接受数据,并放到数据包中

DatagramSocket ds=new DatagramSocket(6666);

while(true){

//接受数据存到数据包中

ds.receive(dp);

System.out.println(new String(buf,0,dp.getLength()));

}

}

}

--------------------------------------------------------------------------------------

import java.net.*;

public class UDPClient{

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",6666));

DatagramSocket ds=new DatagramSocket(9999);

ds.send(dp);

ds.close();

}

}

====================================================================================================

import java.net.*;

import java.io.*;

public class UDPServer{

public static void main(String args[]) throws Exception{

byte buf[]=new byte[1024];

//创建数据包,而数据包中真正存数据的是上面的数组

DatagramPacket dp=new DatagramPacket(buf,buf.length);

//创建一个DatagramSocket(数据包插座)---接受数据,并放到数据包中

DatagramSocket ds=new DatagramSocket(6666);

while(true){

//接受数据存到数据包中

ds.receive(dp);

ByteArrayInputStream bais=new ByteArrayInputStream(buf);

DataInputStream dis=new DataInputStream(bais);

long n=dis.readLong();

System.out.println(n);

}

}

}

---------------------------------------------------------------------------------------------------

import java.net.*;

import java.io.*;

public class UDPClient{

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();

DatagramPacket dp=new DatagramPacket(buf,buf.length,

new InetSocketAddress("127.0.0.1",6666));

DatagramSocket ds=new DatagramSocket(9999);

ds.send(dp);

ds.close();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: