网络编程经验总结

网络编程经验总结
网络编程经验总结

小乐网络编程经验总结

首先我想问一下什么是网络通信协议呢?简单的说就是说的是同一种语言,你为什么可以和说普通话的人沟通呢,他说的话你能够听的懂,并且你说的话他也能够听懂,

什么是计算机网络呢?

?把分布在不同地理区域的计算机与专门的外部设备用通信线路互联成一个规模大、功能强的网络系统,从而使众多的计算机可以方便地互相传递信息,共享硬件、软件,数据信息等资源。

?计算机网络的主要功能

?资源共享

?信息传输与集中处理

?均衡负荷与分布处理

?综合信息服务(www/综合业务数字网络ISDN)

?什么是网络通信协议

?计算机网络中实现通信必须有一些约定即通信协议,对速率、传输代码、代码结构、传输控制步骤、出错控制等制定标准。

?网络通信接口

?为了使两个结点能进行对话,必须在他们之间建立通信工具(即接口),使彼此之间能进行信息交换。接口包括两部分:

?硬件装置:实现结点之间的信息传送

?软件装置:规定双方进行通信的约定协议

你机器为什么能和他的机器连在一起呀,你机器和他机器是通过同一个网关上的网,

分层的思想,每一层只跟自己的下一层联系,

OSI(开放系统互联)

应用层

表示层

会话层

传输层

网络层

数据链路层

物理层

Tcp/IP参考模型—是我们网络编程所要用到的事实上的标准

应用层

传输层

网络层

物理+数据链路层

数据封装:打包送到下一层,一直送到物理层(底层)变成010101送出去

数据拆封:一层一层往上翻译,一直到应用层,读出来

掌握分层的思想有巨大的好处,每一层都只和下一层打交道,每一层都是相对独立的,为什么我们只是学习应用程序编程呀,因为我们通常是在应用层编写代码,最多和表示层有点关系,表示层做展现,JSP文件还有数据链路层,JDBC连接池,Hibernate,

IP地址:分子网,A(1-126) B(128-191) C (192-223)

子网掩码:后面主机号不一样,前面的IP地址一样,这样就能够访问同一个网络,

网关:通过同一个接口上网,我这台机器的IP地址也未必是真实的,你们要想上网,就得连上我这台机器,我作为网关再连上电信的主机,想要发布网站,就得发布在公网上,当然有些软件能够自动地讲其从内网发布到公网上

下面谈网络编程必须要熟悉的TCP/UDP

TCP协议和UDP协议

?TCP(transmission control protocol)

?是专门设计用于在不可靠的因特网上提供可靠的、端到端的字节流通信的协议。它是一种面向连接的协议。TCP连接是字节流而非报文流。

?UDP(User data protocol)

?UDP向应用程序提供了一种发送封装的原始IP数据报的方法、并且发送时无需建立连接,是一种不可靠的连接。

Socket

?两个java应用程序可通过一个双向的网络连接实现数据交换,这个双向链路的一端称为一个Socket。

?Socket通常用来实现client-server连接。

?https://www.360docs.net/doc/d25727443.html,包中定义的两个类Socket和ServerSocket分别用来实现双向连接的client和server端

?建立连接时所需的寻址信息为远程计算机的IP地址和端口号(Port number)

?TCP端口UDP端口分开的

?每一个65536个端口

银行转账系统一般是TCp的,需要验证的一类程序一般都是TCP的,而像网络游戏和聊天程序等一般都是UDP的,UDP效率高,大不了就网卡一下,丢包呗,或是声音吱啦两声,或是几个马赛克,这些都无所谓,只要网络基本可靠就行。

网络:

1、TCP 字节流,可靠的UDP 数据报,不可靠的

2、Socket:服务端和客户端一起写,服务端要先启动,服务端检听某个端口,客户端连接服务器IP和某个端口

3、UDP:服务端,byte[] DatagramPacket(b,b.length) DatagramSocket(服务器检听的端口号) 客户端,byte[] DatagramPacket(b,b.length,newInetSocketAddress("服务器IP",客户端发送端口)) DatagramSocket(客户端发往服务器端的端口号)

TCPServer:

package TCPUDP;

import https://www.360docs.net/doc/d25727443.html,.*;

import java.util.Scanner;

import java.io.*;

publicclass TCPServer {

publicstaticvoid main(String[] args)throws Exception{

ServerSocketss=new ServerSocket(6666);

Socket s=ss.accept();

InputStream is=s.getInputStream();

OutputStreamos=s.getOutputStream();

while(true){

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

DataInputStream dis=new DataInputStream(is);

System.out.println("client:"+dis.readInt());

DataOutputStream dos=new DataOutputStream(os);

int i=0;

dos.writeInt(i++);

dos.flush();

}

}

}

TCPClient:

import https://www.360docs.net/doc/d25727443.html,.*;

import java.io.*;

importjava.util.*;

public class TCPClient {

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

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

OutputStreamos=s.getOutputStream();

InputStream is=s.getInputStream();

while(true){

DataOutputStream dos=new DataOutputStream(os);

Random rd=new Random();

int i=rd.nextInt(10);

dos.writeInt(i);

//int i=0;

//dos.writeUTF("hello"+(i++));

dos.flush();

DataInputStream dis=new DataInputStream(is);

System.out.println("server:"+dis.readInt());

}

}

}

TalkServer:

Socket socket=ss.accept();

ts.br=new BufferedReader(new InputStreamReader(socket.getInputStream()));

ts.rw=new PrintWriter(socket.getOutputStream());

MysFramemysf=new MysFrame(ts);

while(true){

mysf.l.setText("Client:"+ts.br.readLine());

}

}catch(Exception e){

e.printStackTrace();

}

}

}

class MysFrame extends Frame{

public TextFieldtf=null;

public Label l=null;

TalkServerts;

MysFrame(TalkServerts){

super("TalkServer");

tf=new TextField(20);

tf.addActionListener(new MysMonitor(ts));

l=new Label("client:");

this.add(tf,"North");

this.add(l,"South");

pack();

setVisible(true);

}

}

class MysMonitor implements ActionListener{

TalkServerts;

MysMonitor(TalkServerts){

this.ts=ts;

}

public void actionPerformed(ActionEvent e){

TextFieldtf=(TextField)e.getSource();

String readline=null;

readline=tf.getText();

tf.setText("");

if(!readline.equals("byebye")){

ts.rw.println(readline);

ts.rw.flush();

System.out.println("Server:"+readline);

}

else{

TalkClient:

package TCPUDP;

import https://www.360docs.net/doc/d25727443.html,.*;

import java.io.*;

importjava.awt.*;

importjava.awt.event.*;

public class TalkClient {

BufferedReaderbR;

PrintWriterrw;

public static void main(String[] args){

try{

TalkClienttc=new TalkClient();

Socket socket=new Socket("localhost",8888);

tc.bR=new BufferedReader(new InputStreamReader(socket.getInputStream()));

tc.rw=new PrintWriter(socket.getOutputStream());

MycFramemycF=new MycFrame(tc);

while(true){

mycF.l.setText("server:"+tc.bR.readLine());

}

}catch(Exception e){

e.printStackTrace();

}

}

}

class MycFrame extends Frame{

public TextFieldtf=null;

public Label l=null;

TalkClienttc;

MycFrame(TalkClienttc){

super("TalkServer");

tf=new TextField(20);

tf.addActionListener(new MycMonitor(tc));

l=new Label("server:");

this.add(tf,"North");

this.add(l,"South");

pack();

}

}

class MycMonitor implements ActionListener{ TalkClienttc;

MycMonitor(TalkClienttc){

this.tc=tc;

}

public void actionPerformed(ActionEvent e){ TextFieldtf=(TextField)e.getSource();

String readline=null;

readline=tf.getText();

tf.setText("");

if(!readline.equals("byebye")){

tc.rw.println(readline);

tc.rw.flush();

System.out.println("client:"+readline);

}

else{

System.exit(0);

}

}

}

TestSocketServer:

mycF.l.setText("server:"+tc.bR.readLine());

}

}catch(Exception e){

e.printStackTrace();

}

}

}

class MycFrame extends Frame{

public TextFieldtf=null;

public Label l=null;

TalkClienttc;

MycFrame(TalkClienttc){

super("TalkServer");

tf=new TextField(20);

tf.addActionListener(new MycMonitor(tc));

l=new Label("server:");

this.add(tf,"North");

this.add(l,"South");

pack();

setVisible(true);

}

}

class MycMonitor implements ActionListener{

TalkClienttc;

MycMonitor(TalkClienttc){

this.tc=tc;

}

public void actionPerformed(ActionEvent e){

TextFieldtf=(TextField)e.getSource();

String readline=null;

readline=tf.getText();

tf.setText("");

if(!readline.equals("byebye")){

tc.rw.println(readline);

tc.rw.flush();

System.out.println("client:"+readline);

}

else{

System.exit(0);

}

}

}

TestSocketClient:

package TCPUDP;

import https://www.360docs.net/doc/d25727443.html,.*;

import java.io.*;

public class TestSocketClient {

public static void main(String[] args){

InputStream is=null;

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

}

}

}

TestUDPServer:

import java.io.*;

public class TestUDPServer {

static byte buf[]=null;

static DatagramPacketdp=null;

static DatagramSocket ds=null;

byte[] bufo=null;

DatagramPacketdpo=null;

DatagramSocketdso=null;

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

TestUDPServerts=new TestUDPServer();

MyUDPServermyServer=new MyUDPServer(ts);

ds=new DatagramSocket(9999);

while(true){

buf=new byte[1024];

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

ds.receive(dp);

String strTa=myServer.ta.getText();

myServer.ta.setText(strTa+"Client:"+new String(buf,0,dp.getLength())+"\n");

//ds.close();

/*ByteArrayInputStreambis=new ByteArrayInputStream(buf);

DataInputStream dis=new DataInputStream(bis);

System.out.println(dis.readLong());*/

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

}

}

}

class MyUDPServer extends Frame{

TextFieldtf=null;

TextArea ta=null;

TestUDPServerts;

MyUDPServer(TestUDPServerts){

super("服务器端");

setBounds(50,50,300,300);

ta=new TextArea();

tf=new TextField();

tf.addActionListener(new ServerMonitor(ts,this));

this.add(ta,"North");

this.add(tf,"South");

this.setVisible(true);

this.addWindowListener(new MyWindowMonitor());

}

}

class ServerMonitor implements ActionListener{

TestUDPServerts;

MyUDPServerms;

ServerMonitor(TestUDPServerts,MyUDPServerms){

this.ts=ts;

this.ms=ms;

}

public void actionPerformed(ActionEvent e){

TextFieldtf=(TextField)e.getSource();

String strTa=ms.ta.getText();

ms.ta.setText(strTa+"Server:"+tf.getText()+"\n");

ts.bufo=(tf.getText()).getBytes();

try{

ts.dpo=new DatagramPacket(ts.bufo,ts.bufo.length,

new InetSocketAddress("localhost",8888));

ts.dso=new DatagramSocket(6789);

ts.dso.send(ts.dpo);

ts.dso.close();

}catch(Exception eee){

eee.printStackTrace();

}

tf.setText("");

}

}

TestUDPClient:

dos.writeLong(n);

byte[] buf=bos.toByteArray();*/

//byte[] buf=(new String("hello")).getBytes();

/*DatagramPacketdp=new DatagramPacket(buf,buf.length,

new InetSocketAddress("localhost",9999));

DatagramSocket ds=new DatagramSocket(5678);

ds.send(dp);

ds.close();*/

TestUDPClienttc=new TestUDPClient();

MyUDPClient mc=new MyUDPClient(tc);

while(true){

bufi=new byte[1024];

dpi=new DatagramPacket(bufi,bufi.length);

dsi=new DatagramSocket(8888);

dsi.receive(dpi);

String strTa=mc.ta.getText();

mc.ta.setText(strTa+"Server:"+new String(bufi,0,dpi.getLength())+"\n");

dsi.close();

}

}

}

class MyUDPClient extends Frame{

TextFieldtf=null;

TextArea ta=null;

TestUDPClienttc;

MyUDPClient(TestUDPClienttc){

super("客户端");

setBounds(50,50,300,300);

ta=new TextArea();

tf=new TextField();

tf.addActionListener(new ClientMonitor(tc,this));

this.add(ta,"North");

this.add(tf,"South");

this.setVisible(true);

this.addWindowListener(new MyWindowMonitor());

}

}

class ClientMonitor implements ActionListener{

TestUDPClienttc;

MyUDPClient mc;

ClientMonitor(TestUDPClienttc,MyUDPClient mc){

this.tc=tc;

this.mc=mc;

}

public void actionPerformed(ActionEvent e){

TextFieldtf=(TextField)e.getSource();

String strTa=mc.ta.getText();

mc.ta.setText(strTa+"Client:"+tf.getText()+"\n");

tc.buf=(tf.getText()).getBytes();

try{

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

new InetSocketAddress("localhost",9999));

tc.ds=new DatagramSocket(5678);

tc.ds.send(tc.dp);

tc.ds.close();

}catch(Exception eee){

eee.printStackTrace();

}

tf.setText("");

}

}

class MyWindowMonitor extends WindowAdapter{

public void windowClosing(WindowEvent e){

System.exit(0);

}

}

相关主题
相关文档
最新文档