java 網路編程 TCP協議 java 伺服器和用戶端 java socket編程

來源:互聯網
上載者:User

標籤:ext   網路   log   rgs   結束   exce   死迴圈   www.   new   

一個 HelloWord 層級的 Java Socket 通訊的例子。通訊過程:
        先啟動 Server 端,進入一個死迴圈以便一直監聽某連接埠是否有串連請求。然後運行 Client 端,用戶端發出串連請求,服務端監聽到這次請求後向用戶端發回接受訊息,串連建立,啟動一個線程去處理這次請求,然後繼續死迴圈監聽其他請求。用戶端輸入字串後按斷行符號鍵,向伺服器發送資料。伺服器讀取資料後回複用戶端資料。這次請求處理完畢,啟動的線程消亡。如果用戶端接收到 "OK" 之外的返回資料,會再次發送串連請求並發送資料,伺服器會為這次串連再次啟動一個線程來進行響應。。。直到當用戶端接收到的返回資料為 "OK" 時,用戶端退出。

 

  Java中重點之一就是伺服器與用戶端的串連,因為是在同一台PC上,所以就設定伺服器的地址為“localhost”,注意,我當時試著設定其他名稱,但都不行,這個原因還請老司機告一下。另外還要注意,服務端先於用戶端運行,廢話不多說,上代碼了:

用戶端:

按 Ctrl+C 複製代碼

package WebProgramingDemo;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class SocketDemo {

/**
* @param args
* @throws IOException
* @throws UnknownHostException
*/
public static void main(String[] args) throws IOException {

Socket s=new Socket("192.168.2.103",10002);
OutputStream out=s.getOutputStream();
out.write("Java".getBytes());
InputStream is=s.getInputStream();
byte buf[]=new byte[1024];
int len=is.read(buf);
System.out.println(new String(buf,0,len));
s.close();
}

}
按 Ctrl+C 複製代碼
服務端:

按 Ctrl+C 複製代碼

package WebProgramingDemo;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerSocketDemo {

/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {

ServerSocket ss = new ServerSocket(10002);
Socket s = ss.accept();
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip + "....connected....");
InputStream in = s.getInputStream();
int len = 0;
byte[] buf = new byte[1024];
len = in.read(buf);
System.out.println(new String(buf, 0, len));
OutputStream os=s.getOutputStream();
os.write("收到".getBytes());
os.close();
s.close();
ss.close();
}

}
按 Ctrl+C 複製代碼

 

http://www.cnblogs.com/ysw-go/p/5323357.html

 

伺服器修改後的代碼:

package test;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class ServerSocketDemo {

/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {

ServerSocket ss = new ServerSocket(10002);
Socket s = ss.accept();

while(true)
{

String ip = s.getInetAddress().getHostAddress();
// System.out.println(ip + "....connected....");
InputStream in = s.getInputStream();
int len = 0;
byte[] buf = new byte[1024];
len = in.read(buf);
System.out.println(new String(buf, 0, len));
OutputStream os=s.getOutputStream();
Scanner scan=new Scanner(System.in);
String s1=scan.next();
os.write(s1.getBytes());
// os.write("收到".getBytes());

// os.close();
// s.close();
// ss.close();
}


}

}

 

 

用戶端代碼修改後:

package test2;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class SocketDemo {

/**
* @param args
* @throws IOException
* @throws UnknownHostException
*/
public static void main(String[] args) throws IOException {


Socket s=new Socket("localhost",10002);
while(true)
{

OutputStream out=s.getOutputStream();
Scanner scan=new Scanner(System.in);
String s1=scan.next();
out.write(s1.getBytes());
//out.write(s1.getBytes());
InputStream is=s.getInputStream();
byte buf[]=new byte[1024];
int len=is.read(buf);
System.out.println(new String(buf,0,len));
// s.close();
}
}

}

 

 

伺服器先開啟,然後開啟用戶端,然後客戶必須先發訊息用套子連結的伺服器,然後伺服器才發訊息回饋用戶端。一次通訊結束。用戶端的輸出對應伺服器的輸入,伺服器的輸出對應用戶端的輸入。

java 網路編程 TCP協議 java 伺服器和用戶端 java socket編程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.