????Java??????
?????????????? ???????[ 2016/9/18 13:18:51 ] ??????????????????? Java
????????????????????????????
?????????????????λ????????????????????????????????????ο????Ч????????????
??????TCP/IPЭ????IP????????????????????λ??????????·?????IP???????????Internet???????????
??????TCP??????????????????tcp??????????UDP???????????????????????????????????????????IP??????δ?????????
????????????е????????????????/????????C/S???????????????????????????????????????????????????????????????????????? ???????????????????????????????У????????????????п?????????????????????????????????????????????????????????????? ???????????
??????????????Э?飺TCP??UDP
????TCP??Tranfer Control Protocol?? ????????????????????????????Э?顣???TCPЭ?鴫?????????????????????????????????????????????????socket?????? ????????????TCPЭ????????????????????socket?????????server socket?????????????????????socket????????????????????????socket?????????????????????????????????????????з??? ??????????
????UDP??User Datagram Protocol?????????????????Э?飬??????????????????????????????????????????????????????????????κο????·??????????????????????????????????????????????????????????????
????????
????UDP??1???????????ж??????????????????????????????????????????????????
????2??UDP????????????д?С??????????????????????????????64KB????
????3??UDP????????????Э?飬????????????????????????????????????????
????TCP??1???????????Э?飬??socket??????????????????????????????????TCP?????????
???????
????2??TCP?????????С??????????????????????????socket?????????????????
?????????
????3??TCP??????????Э?飬????????????????????????????????????????
????????
????1??TCP????????????м????????????????????????Telnet???????????FTP????????????????????????????????????????????????????????????????????????????ü?????????????????????????TCP?????Ч?????UDP???
????2??UDP????????????????????????????????????????????????????client/server??ó?????????????????????????????????????????????????????????????????????????UDP????????Щ??
????????????Socket??java??????
????1??????Socket
????????????????????????????????????????????????????????·??????????Socket??Socket??????????????????????????Socket??TCP/IPЭ????????????е?????棬???Socket?????IP?????????????????
?????????Socket??????Э???????????TCP/IP??????????????????б??????????Java?????£?Socket?????????????TCP/IPЭ??????????
????2??Socket???????
????Server??Listen(????)???????????????????Client????Server ?????Connect(????)????Server????Client?????Accept?????????????????????????????Server???Client ??????????Send??Write?????????????
????????????????????Socket?????????????????????乤????????????????????????裺
??????1?? ????Socket??
??????2?? ???????Socket??????/??????
??????3?? ?????????Э???Socket???ж?/д??????
??????4?? ???Socket.???????????У???δ?????????close?????????????????????????????????У??????????????????????????????δ??????????
????3??????Socket
????????Socket
????java???java.net????????????Socket??ServerSocket??????????????????????????????????????????÷?????????ú?????乹???????£?
????Socket(InetAddress address?? int port);
????Socket(InetAddress address?? int port?? boolean stream);
????Socket(String host?? int prot);
????Socket(String host?? int prot?? boolean stream);
????Socket(SocketImpl impl)
????Socket(String host?? int port?? InetAddress localAddr?? int localPort)
????Socket(InetAddress address?? int port?? InetAddress localAddr?? int localPort)
????ServerSocket(int port);
????ServerSocket(int port?? int backlog);
????ServerSocket(int port?? int backlog?? InetAddress bindAddr)
????????address??host??port?????????????????????IP?????????????? ????stream???socket????socket?????????socket??localPort?????????????????localAddr?? bindAddr??????????????ServerSocket?????????????impl??socket??????????????????serverSocket??? ??????????Socket??count?????????????????????????????磺??????? http://www.xxspw.com
????Socket client = new Socket("127.0.01."?? 80);
????ServerSocket server = new ServerSocket(80);
?????????????????????С?????????????????????????и????????????? ????????????0~1023???????????????????http?????????80??telnet?????????21??ftp?????????23?? ???????????????????????????????1023?????????????????
?????????socket??????????????????IOException????????б???????????????????????Socket??ServerSocket???????????????
????4??????Client/Server????
????(1). ????????
import java.io.*;
import java.net.*;
public class TalkClient {
public static void main(String args[]) {
try{
Socket socket=new Socket("127.0.0.1"??4700);
//??????4700?????????????
BufferedReader sin=new BufferedReader(new InputStreamReader(System.in));
//????????????豸????BufferedReader????
PrintWriter os=new PrintWriter(socket.getOutputStream());
//??Socket???????????????????PrintWriter????
BufferedReader is=new BufferedReader(new InputStreamReader(socket.getInputStream()));
//??Socket?????????????????????????BufferedReader????
String readline;
readline=sin.readLine(); //????????????????????
while(!readline.equals("bye")){
//??????????????????? "bye"???????
os.println(readline);
//???????????????????????????Server
os.flush();
//???????????Server??????????????
System.out.println("Client:"+readline);
//????????????????????????
System.out.println("Server:"+is.readLine());
//??Server???????????????????????????
readline=sin.readLine(); //????????????????????
} //???????
os.close(); //???Socket?????
is.close(); //???Socket??????
socket.close(); //???Socket
}catch(Exception e) {
System.out.println("Error"+e); //????????????????
}
}
}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11