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

Socket Programming

时间:2017-09-17 13:37:22      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:read   tor   ide   print   href   use   dev   tcp   little   

| 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语言实现):

//: MyServer.java
import java.io.*;
import java.net.*;

public class MyServer{
  public static void main(String[]args) {
    try {
      ServerSocket ss = new ServerSocket(6666);
      Socket s = ss.accept();

      DataInputStream dis = new DataInputStream(s.getInputStream());
      DataOutputStream dos = new DataOutputStream(s.getOutputStream());

      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

      String str = "";
      while (!str.equals("stop")) {

        str = (String)dis.readUTF();
        System.out.println("[client] " + str);

        str = br.readLine();
        System.out.println("[server] " + str);
        dos.writeUTF(str);
        dos.flush();

      }

      s.close();
      ss.close();

    } catch (Exception e){
      System.out.println(e);
    }
  }
}
//: MyClient.java
import java.io.*;
import java.net.*;

public class MyClient {
  public static void main (String []args) {
    try {
      Socket s = new Socket("localhost", 6666);
      DataOutputStream dos = new DataOutputStream(s.getOutputStream());
      DataInputStream dis = new DataInputStream(s.getInputStream());

      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      String str = "";

      while(!str.equals("stop")) {
        str = br.readLine();
        dos.writeUTF(str);
        dos.flush();
        System.out.println("[client] " + str);

        System.out.println("[server] " + dis.readUTF());
      }

      dos.close();
      s.close();
    } catch (Exception e) {
      System.out.println(e);
    }

  }
}




Socket Programming

标签:read   tor   ide   print   href   use   dev   tcp   little   

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

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