TCP协议是一个有连接可靠地协议。
TCP编程的核心思路
开发服务器端
ServerSocket ss=new ServerSocket(9000)
ss.accept();
public
class
TcpServer {
public
static
void
main(String[] args) {
try
{
ServerSocket ss=
new
ServerSocket (9000);
//创建SocketServer对象,并绑定端口
Socket s=ss.accept();
BufferedReader br=
new
BufferedReader(
new
InputStreamReader(s.getInputStream()));
//调用getInputStream方法,进行I/O操作
String str=br.readLine();
System.
out
.println(
"接收的数据为:"
+str);
s.close();
}
catch
(IOException e) {
e.printStackTrace();
}
}
}
public class TcpServer02 {
public static void main(String[] args) {
try {
ServerSocket ss= new ServerSocket(9000);//创建SocketServer对象,并绑定端口
Socket s=ss.accept();
BufferedReader br= new BufferedReader(new InputStreamReader(s.getInputStream()));//调用getInputStream方法,进行I/O操作
String str=br.readLine();
System. out.println("接收的数据为:" +str );
PrintWriter pw= new PrintWriter(s.getOutputStream());
pw.println( str+"from Server");
pw.flush();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
开发客户端
Socket s=new Socket("150.236.56.101",9000);
public class TcpClient {
public static void main(String[] args) {
try {
Socket s= new Socket("localhost" ,9000);//创建Socket对象,并连接服务器
PrintWriter pw= new PrintWriter(s.getOutputStream());//调用getOutputStream方法,进行I/O操作
pw.println( "Hello world!");
pw.flush(); //将缓冲区里的数据向外发出
s.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class TcpClient02 {
public static void main(String[] args) {
try {
Socket s= new Socket("localhost" ,9000);//创建Socket对象,并连接服务器
PrintWriter pw= new PrintWriter(s.getOutputStream());//调用getOutputStream方法,进行I/O操作
pw.println( "Hello world!");
pw.flush(); //将缓冲区里的数据向外发出
BufferedReader br= new BufferedReader(new InputStreamReader(s.getInputStream()));
String str=br.readLine();
System. out.println("接收服务器返回信息:" +str);
s.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
以上程序存在的问题,服务器只能运行一次
while(true){
调用accept()方法
调用getXXXStream方法,进行I/O操作
关闭Socket
}
像以上那样加入一个死循环,也不能达到目的,所以此时可以运用线程来操作
public
class
ServerThread
extends
Thread{
Socket
s
;
public
ServerThread(Socket s) {
this
.
s
=s;
}
public
void
run() {
//实现run()方法
try
{
BufferedReader br=
new
BufferedReader(
new
InputStreamReader(
s
.getInputStream()));
//调用getInputStream方法,进行I/O操作
String str=br.readLine();
System.
out
.println(
"接收的数据为:"
+str);
PrintWriter pw=
new
PrintWriter(
s
.getOutputStream());
pw.println(str+
"from Server"
);
pw.flush();
}
catch
(IOException e) {
e.printStackTrace();
}
finally
{
try
{
s
.close();
}
catch
(IOException e) {
e.printStackTrace();
}
}
}
}
public class TcpServer03 {
public static void main(String[] args) throws IOException {
ServerSocket ss= new ServerSocket(9000);
while(true ){
Socket s=ss.accept();
Thread st= new ServerThread(s);
st.start();
}
}
}
UDP:无连接,不可靠(应用:网络视频聊天)
UDP编程
java.net.DatagramSocket
java.net.DatagramPacket
UDP编程的核心思路
服务器 DatagramSocket socket=new DatagramSocket(9000)
客户端 DatagramSocket socket=new DatagramSocket()
DatagramPacket
DatagramPacket(byte[] buf,int,length)DatagramPacket(byte[] buf,int,length,SocketAddress)getAddress()/getSockAddress()/getPort()/getLength()setAddress()/setSockAddress()/setPort()/setLength()
public class UdpClient {
public static void main(String[] args) throws IOException {
DatagramSocket socket= new DatagramSocket();
String str= "Hello World!";
byte[] buf=str.getBytes();
DatagramPacket packet= new DatagramPacket(buf,0,buf.length ,new InetSocketAddress("localhost" ,9000));
socket.send(packet);
socket.close();
}
}
public class UdpServer {
public static void main(String[] args) throws IOException {
DatagramSocket socket= new DatagramSocket(9000);
byte[] buf=new byte[100];
DatagramPacket paper= new DatagramPacket(buf,0,buf.length );
socket.receive(paper);
String str=new String(buf,0,paper.getLength());
System. out.println(str);
socket.close();
}
}
URL概念(统一资源定位符
)
URL组成
协议名://主机名(或者IP地址):端口号/资源
如:http://localhost:8080/web/a.jpg;
URL编程
URL
URLConnection
public class URLTest {
public static void main(String[] args) throws IOException {
URL url= new URL("http://www.Oracle.com" );
URLConnection conn=url.openConnection();
InputStream in=conn.getInputStream();
BufferedReader br= new BufferedReader(new InputStreamReader(in));
String line= null;
while((line=br.readLine())!=null){
System. out.println(line);
}
br.close();
}
}
转载于:https://blog.51cto.com/5028233/857026