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();
}
}
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();
}
}
相关文章推荐
- 网络编程[21]
- 27-网络编程-21-网络编程(了解客户端和服务器端原理)
- C# 网络编程之通过ip地址获取地理位置(补充)
- 网络编程(21)—— 使用epoll进行IO复用
- vc++网络安全编程范例(21)用CryptoAPI进行数据加密
- Boost.Asio c++ 网络编程翻译(21)
- 8月22号 JDBC,线程,网络编程补充
- 网络编程中的概念再补充
- QT编程系列11--Qt网络编程的补充(一)
- Java基础知识强化之网络编程笔记21:Android网络通信之 Android常用OAuth登录(获取令牌信息)
- 网络编程-基于UDP的Socket网络编程(补充)
- C# 网络编程之通过ip地址获取地理位置(补充)
- C#编程入门21_网络编程
- 21 网络编程
- Boost.Asio c++ 网络编程翻译(21)
- 大数据预科20(补充之网络编程)
- 黑马程序员—21—java基础:有关网络编程的学习笔记和学习心得体会
- Java网络编程从入门到精通(21):HTTP消息的格式
- Java网络编程(21):HTTP消息的格式
- 第九篇:网络编程补充与进程