码迷,mamicode.com
首页 > 其他好文 > 详细

Socket Programming

时间:2017-03-08 21:18:43      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:rip   pac   []   val   nes   cat   port   oid   sys   

| There are two ways to store this value.

  • Little Endian.(低位优先)
  • Big Endian.(高位优先)

| The complete Client and Server interaction.
技术分享

| The simplest way to write a concurrent server under Unix is to fork a child process to handle each client separately.

| Socket Type

  • Stream Sockets(TCP)[most commonly used]
  • Datagram Sockets (UDP)[most commonly used]
  • Raw Sockets (Raw Sockets are not intended for the general user; they have been provided mainly for those interested in developing new communication protocols, of for gaining access to some of the more cryptic facilities of an existing protocol.) [rarely used]
  • Sequenced Packet Socket [rarely used]

| Where is socket used?
A Unix socket is used in a client-server application framework.

| In Unix, every I/O action is done by writing or reading a file descriptor.

| Socket allow communication between two different processes on the same or different machines.

| Unix Socket
https://www.tutorialspoint.com/unix_sockets/


| Connection-oriented socket (TCP)

技术分享

| Connectionless socket (UDP)
技术分享

| 你一言我一语(Java语言实现):

  1. //: MyServer.java
  2. import java.io.*;
  3. import java.net.*;
  4. public class MyServer{
  5. public static void main(String[]args) {
  6. try {
  7. ServerSocket ss = new ServerSocket(6666);
  8. Socket s = ss.accept();
  9. DataInputStream dis = new DataInputStream(s.getInputStream());
  10. DataOutputStream dos = new DataOutputStream(s.getOutputStream());
  11. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  12. String str = "";
  13. while (!str.equals("stop")) {
  14. str = (String)dis.readUTF();
  15. System.out.println("[client] " + str);
  16. str = br.readLine();
  17. System.out.println("[server] " + str);
  18. dos.writeUTF(str);
  19. dos.flush();
  20. }
  21. s.close();
  22. ss.close();
  23. } catch (Exception e){
  24. System.out.println(e);
  25. }
  26. }
  27. }
  1. //: MyClient.java
  2. import java.io.*;
  3. import java.net.*;
  4. public class MyClient {
  5. public static void main (String []args) {
  6. try {
  7. Socket s = new Socket("localhost", 6666);
  8. DataOutputStream dos = new DataOutputStream(s.getOutputStream());
  9. DataInputStream dis = new DataInputStream(s.getInputStream());
  10. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  11. String str = "";
  12. while(!str.equals("stop")) {
  13. str = br.readLine();
  14. dos.writeUTF(str);
  15. dos.flush();
  16. System.out.println("[client] " + str);
  17. System.out.println("[server] " + dis.readUTF());
  18. }
  19. dos.close();
  20. s.close();
  21. } catch (Exception e) {
  22. System.out.println(e);
  23. }
  24. }
  25. }




Socket Programming

标签:rip   pac   []   val   nes   cat   port   oid   sys   

原文地址:http://www.cnblogs.com/lyloou/p/6522953.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!