A. 關於java socket編程
先運行伺服器端類,然後再運行客戶端類,就可以了
/**
*伺服器端類
*/
public class Server {
public static void main(String[] args) throws IOException{
Server server = new Server();
server.start();
}
public void start() throws IOException{
//ServerSocket 對當前伺服器的服務埠的綁定
//這個埠號不能重復綁定,不能同時執行兩邊
ServerSocket ss = new ServerSocket(8888);
while(true){
//accept 開始等待(IO Block)客戶連接(啟動監聽),如果沒有客戶端連接,一直掛起等待下去。
//如果有客戶端連接,才會繼續執行下去,返回的Socket實例s 代表對於客戶端連接。
Socket s = ss.accept();
//創建並啟動客戶服務線程,為客戶服務
//當前線程再次返回到accept等待,下一個客戶連接
new Service(s).start();//創建線程
}
}
class Service extends Thread{
Socket s;
public Service(Socket s){
this.s = s;
}
public void run(){
try{
//s代表客戶端
//s 中的in代表從客戶傳遞過來的流
//s 中的out代表從伺服器到客戶端傳輸流
InputStream in = s.getInputStream();
Scanner sc = new Scanner(in);//System.in是操作系統後台
OutputStream out = s.getOutputStream();
//out.write("您好!您需要點啥?\n".getBytes("GBK"));
//out.flush();//清理緩沖,確保發送到客戶端
while(true){
String str = sc.nextLine();//IO Block
if(str.equals("連接伺服器")){
out.write("連接成功!\n".getBytes("GBK"));
out.flush();
break;
}
}
}catch(IOException e){
e.printStackTrace();
}
}
}
}
/**
*客戶端類
*/
public class Client {
public static void main(String[] args) throws IOException{
// new Socket() 連接到指定的伺服器埠,當前用的是本機的埠
Socket s = new Socket("localhost", 8888);
//返回s代表連接到了伺服器
//s代表對伺服器的連接
InputStream in = s.getInputStream();
OutputStream out = s.getOutputStream();
out.write("連接伺服器\n".getBytes("gbk"));
out.flush(); //清理緩沖,確保發送到服務端
Scanner sc = new Scanner(in);
String str = sc.nextLine();
System.out.println(str); //把從伺服器返回的信息,列印到控制台。
out.flush();
}
}
B. 請問,Java網路編程中像socket以及多線程這些,哪些地方用到現在工作基本都用框架,感覺即使
網路編程的前景很不錯的,隨著移動互聯網的普及與發展,手機應用不回斷深入我們生活的方方答面面,移動系統和軟體應用的開發領域,成為了未來很具有潛力的行業之一。
在未來5年內,合格軟體人才的需求將遠大於供給。其中,尤以JAVA、PHP、HTML5與CSS3開發、Asp.net開發人才最為缺乏。
C. java socket編程怎麼實現
分為兩種模式:TCP和UDP
TCP有客戶端(Client)和服務端(Server)的概念,服務端指定一個ip和埠(port)並且監聽是否有客戶端接入,客戶端通過ip+port連接到服務端,通過socket得到輸入輸出流,他們之間的通信都是通過流(Stream)進行的,一個服務端可以接入多個客戶端,客戶端之間的通信依賴於服務端的轉發。
UDP並沒有嚴格的客戶端和服務端的區別,你只需要建立一個socket指定你要發送的目標ip和port就行了,他是以數據包的形式發送數據(DataPacket),數據包裡面存放的是byte[],有需要的話可補充具體實現源碼
D. java socket編程 readline()讀取問題,為啥在client端剛連接上就開始無限循環,停不下來
改一下
scan.nextLine();//阻塞,排除是socket建立太慢導致的
String str=null;
這兩行刪除,沒用
str=br.readLine();
這個改成
br=scan.readLine();
然後你再試一下
順便再說一下,這個程序我寫過,我開始想的也是客戶端發送的數據伺服器端可以實時接收,但是我發現我錯了,因為Scanner 的阻塞,如果客戶端發送消息伺服器端必須也得發送一個消息才能收到,這個內容可以是任意內容,比如一個回車,如果想讓客戶端 或伺服器端可以實時接收到消息,在控制台中是不可能實現的,除非使用swing框架,一個文本框負負責發送,一個文本框負責接收,不過聽他們說swing現在企業中根本不用了,所有我也就沒有深入研究,勸你也放棄吧,研究這個沒什麼用,除非你能在網頁上實現,或者做一個程序,至於我寫的源碼,如果你要我可以發給你
對了再說下你這個問題出現的原因
你這個是因為循環中沒有阻塞語句,而且br沒有接收到值,所有它會一直列印null
E. Java需要學習哪些內容
第一:對於Java面向對象概念的理解。學習Java的第一個難點就是如何理解Java的面向對象回編程概念,涉及到類、接答口、多態等概念。在理解這些Java開發概念的過程中,應該從Java面向對象編程過程入手,涉及到類的定義、對象的創建和方法的調用,在這個基礎上可以逐漸形成自身的Java編程思想。
第二:對於Java模塊化編程的理解。Java是抽象程度比較高的編程語言,所以對於初學者來說,可以先從模式編程入手,然後進一步提升自身對於模塊化編程的理解。對於Java程序員來說,模塊化編程能力對於自身的Java開發能力有非常直接的影響。另外,在理解Java模塊化編程的過程中,也會逐漸提升自身的抽象能力。
第三:Java的開發框架。Java開發與開發框架有非常緊密的聯系,初學者可以先從Spring開發框架開始學習,比如從SpringBoot開始學起就是不錯的選擇。Spring系列框架當前已經非常龐大了,基於Spring也能夠完成「一站式」解決方案。Spring框架的學習過程並不復雜,但是需要完成一系列實驗。
F. java socket網路編程
//==============Server.java=================//
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket s = new ServerSocket(12345);
System.out.println("伺服器就緒,請啟動客戶端.");
Socket so = s.accept();
byte[] buff = new byte[1024];
int read = so.getInputStream().read(buff);
String[] abc=new String(buff,0,read).split("\\D+");
int a = Integer.parseInt(abc[0]);
int b = Integer.parseInt(abc[1]);
int c = Integer.parseInt(abc[2]);
if(!cbt(a,b,c))
so.getOutputStream().write("輸入的數據無法組成三角形.".getBytes());
else
so.getOutputStream().write(getArea(a,b,c).getBytes());
so.getOutputStream().flush();
so.close();
s.close();
}
private static String getArea(int a, int b, int c) {
float s = (a+b+c)/2f;
return "面積: "+Math.sqrt(s*(s-a)*(s-b)*(s-c));
}
private static boolean cbt(int a, int b, int c) {
return a>0&&b>0&&c>0&&a+b>c&&b+c>a&&a+c>b;
}
}
//=================Client.java======================//
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client {
public static void main(String[] args) throws UnknownHostException, IOException {
System.out.println("輸入三角形的三邊並用逗號隔開,如: (3,4,5) ");
byte[] buff=new byte[64];
int r = System.in.read(buff);
String ipaddr = "localhost";//根據情況改變,在本機調試就不改了
Socket so = new Socket(ipaddr,12345);
so.getOutputStream().write(new String(buff,0,r).getBytes());
r = so.getInputStream().read(buff);
so.close();
String rs = new String(buff,0,r);
System.out.println(rs);
}
}
//先啟動Server,再啟動Client
G. Java的Socket編程
要通信首先要建立socket鏈接。
1 ab客戶端與服務端建立socket鏈接
2 a客戶端發送消息到服務端
3 服務端收到消息後,發送到指定的b客戶端
4 b客戶端處理來自服務端的消息
H. 通過java編程中socket應用,編寫一個基於c/s架構的區域網通信軟體,
//伺服器端
importjava.io.*;
importjava.net.*;
importjava.util.*;
publicclassChatServer{
booleanstarted=false;
ServerSocketss=null;
Socketsocket=null;
List<Client>clients=newArrayList<Client>();
publicstaticvoidmain(String[]args){
newChatServer().start();
}
publicvoidstart(){
try{
ss=newServerSocket(8888);
started=true;
}catch(BindExceptione){
System.out.println("埠使用中。。。。。");
System.out.println("請關掉相關程序,並重新運行伺服器!");
System.exit(0);
}catch(IOExceptione){
e.printStackTrace();
}
try{
while(started){
socket=ss.accept();
Clientc=newClient(socket);
newThread(c).start();
clients.add(c);
}
}catch(IOExceptione){
e.printStackTrace();
}
}classClientimplementsRunnable{
privateSocketsocket=null;
privateDataInputStreamdis=null;
privateDataOutputStreamdos=null;
privatebooleanbConnected=false;
publicClient(Socketsocket){
bConnected=true;
this.socket=socket;
try{
dis=newDataInputStream(socket.getInputStream());
dos=newDataOutputStream(socket.getOutputStream());
}catch(IOExceptione){
e.printStackTrace();
}
}
publicvoidsend(Stringstr){
try{
dos.writeUTF(str);
}catch(IOExceptione){
clients.remove(this);
System.out.println("對方退出了,我從List裡面去掉了!");
}
}
@Override
publicvoidrun(){
try{
while(bConnected){
Strings=dis.readUTF();
System.out.println(s);
for(inti=0;i<clients.size();i++){
Clientc=clients.get(i);
c.send(s);
}
}
}catch(EOFExceptione){
System.out.println("clientclosed!");
}catch(IOExceptione){
e.printStackTrace();
}finally{
try{
if(dis!=null)dis.close();
if(dos!=null)dos.close();
if(socket!=null)socket.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
}
}
}
//客戶端
importjava.awt.event.*;
importjava.awt.*;
importjava.io.*;
importjava.net.*;
{
TextFieldtfTxt=newTextField();
TextAreataContent=newTextArea();
Socketsocket=null;
DataOutputStreamdos=null;
DataInputStreamdis=null;
booleanbConnected=false;
ThreadtRecv=newThread(newServer());
publicstaticvoidmain(String[]args){
newChatClient().launchFrame();
}
publicvoidlaunchFrame(){
setBounds(400,300,300,300);
setVisible(true);
this.setTitle("ChatClient");
this.add(tfTxt,BorderLayout.SOUTH);
this.add(taContent,BorderLayout.NORTH);
this.pack();
this.addWindowListener(newWindowAdapter(){
@Override
publicvoidwindowClosing(WindowEvente){
disconnect();
setVisible(false);
System.exit(0);
}
});
tfTxt.addActionListener(newTFMonitor());
connect();
tRecv.start();
}
publicvoidconnect(){
try{
socket=newSocket("127.0.0.1",8888);
dos=newDataOutputStream(socket.getOutputStream());
dis=newDataInputStream(this.socket.getInputStream());
System.out.println("Connected!");
bConnected=true;
}catch(UnknownHostExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}
}
publicvoiddisconnect(){
try{
dos.close();
dis.close();
socket.close();
}catch(IOExceptione){
e.printStackTrace();
}
/*
try{
bConnected=false;
tRecv.join();
}catch(InterruptedExceptione){
e.printStackTrace();
}finally{
try{
dos.close();
dis.close();
socket.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
*/
}
{
@Override
publicvoidactionPerformed(ActionEvente){
Stringstr=tfTxt.getText().trim();
//taContent.setText(str);
tfTxt.setText("");
try{
//System.out.println(socket);
dos.writeUTF(str);
dos.flush();
//dos.close();
}catch(IOExceptione1){
e1.printStackTrace();
}
}
}
{
@Override
publicvoidrun(){
try{
while(bConnected){
Strings=dis.readUTF();
//System.out.println(s);
taContent.setText(taContent.getText()+s+' ');
}
}catch(IOExceptione){
System.out.println("talkover,byebye");
//e.printStackTrace();
}
}
}
}
I. java簡單的socket框架實現字元串連接,應該怎麼做
///簡單的Client/Server程序設計
//服務端
import java.io.*;
import java.net.*;
public class Service {
public static void main(String args[]) {
try {
ServerSocket server = null;
try {
server = new ServerSocket(4700);
// 創建一個ServerSocket在埠4700監聽客戶請求
} catch (Exception e) {
System.out.println("can not listen to:" + e);
// 出錯,列印出錯信息
}
System.out.println("server------------------------------");
Socket socket = null;
try {
socket = server.accept();
// 使用accept()阻塞等待客戶請求,有客戶
// 請求到來則產生一個Socket對象,並繼續執行
} catch (Exception e) {
System.out.println("Error." + e);
// 出錯,列印出錯信息
}
String line;
BufferedReader is = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
// 由Socket對象得到輸入流,並構造相應的BufferedReader對象
PrintWriter os = new PrintWriter(socket.getOutputStream());
// 由Socket對象得到輸出流,並構造PrintWriter對象
BufferedReader sin = new BufferedReader(new InputStreamReader(
System.in));
// 由系統標准輸入設備構造BufferedReader對象
System.out.println("Client:" + is.readLine());
// 在標准輸出上列印從客戶端讀入的字元串
line = sin.readLine();
// 從標准輸入讀入一字元串
while (!line.equals("bye")) {
// 如果該字元串為 "bye",則停止循環
// 向客戶端輸出該字元串
os.println(line);
// 刷新輸出流,使Client馬上收到該字元串
os.flush();
// 從Client讀入一字元串,並列印到標准輸出上
System.out.println("Client:" + is.readLine()+"\n");
line = sin.readLine();
// 從系統標准輸入讀入一字元串
} // 繼續循環
os.close(); // 關閉Socket輸出流
is.close(); // 關閉Socket輸入流
socket.close(); // 關閉Socket
server.close(); // 關閉ServerSocket
} catch (Exception e) {
System.out.println("Error:" + e);
// 出錯,列印出錯信息
}
}
}
//客戶端
import java.io.*;
import java.net.*;
public class Client {
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()));
System.out.println("Client------------------------------");
// 由Socket對象得到輸入流,並構造相應的BufferedReader對象
String readline;
readline = sin.readLine(); // 從系統標准輸入讀入一字元串
while (!readline.equals("bye")) {
// 若從標准輸入讀入的字元串為 "bye"則停止循環
os.println(readline);
// 將從系統標准輸入讀入的字元串輸出到Server
os.flush();
// 刷新輸出流,使Server馬上收到該字元串
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); //出錯,則列印出錯信息
}
}
}
//自己去研究代碼 還是挺詳細的
希望能夠幫助到你,望採納!