पर एकाधिक बाइट सरणी भेजना मैं क्लाइंट और सर्वर से एकाधिक बाइट सरणी भेजना चाहता हूं?सॉकेट
मैं भेजने के लिए/ग्राहक से एक बाइट सरणी प्राप्त करें और भेजें/सर्वर से एक बाइट सरणी प्राप्त कर रहा था:
मेरे कोड इस तरह है:
सर्वर:
Socket sock=null;
ByteArrayOutputStream input=null;
OutputStream out=null;
InputStream in=null;
try{
ServerSocket server_sock=new ServerSocket(2972);
sock=server_sock.accept();
in =
sock.getInputStream();
out=sock.getOutputStream();
}catch(IOException e){
System.out.println(e.getMessage());
}
String word="";
//1-Receive
try{
ByteArrayOutputStream serverinput=new ByteArrayOutputStream();
int len=0;
byte[] buf=new byte[1000];
while ((len = in.read(buf))>=0) {
serverinput.write(buf, 0, len);
}
sock.shutdownInput();
word=new String(serverinput.toByteArray());
System.out.println("Client send 1"+word);
}catch(Exception e){
System.out.println(e.getMessage());
}
String st="Server is a king";
try{
out.write(st.getBytes());
out.flush();
}catch(Exception e){
System.out.println(e.getMessage());
}
ग्राहक:
Socket sock=null;
OutputStream out=null;
InputStream in=null;
try{
sock=new Socket("127.0.0.1",2972);
}catch(IOException e){
System.out.println(e.getMessage());
}
String word="Hellow World" ;
try{
in =
sock.getInputStream();
out=sock.getOutputStream();
}catch(IOException e){
System.out.println(e.getMessage());
}
//1- send
try{
System.out.println("Your string is"+word+"converted to byte"+word.getBytes());
out.write(word.getBytes());
out.flush();
sock.shutdownOutput();
}catch(Exception e){
System.out.println(e.getMessage());
}
try{ ByteArrayOutputStream serverinput=new ByteArrayOutputStream();
int len=0;
byte[] buf=new byte[1000];
while ((len = in.read(buf))>=0) {
serverinput.write(buf, 0, len);
}
System.out.println("server send 1 "+new String(serverinput.toByteArray()));
System.out.println("Your string is"+word+"converted to byte"+word.getBytes());
}catch(Exception e){
System.out.println(e.getMessage());
}
यह कोड क्लाइंट और सर्वर से सबमिट करने के लिए ठीक काम कर रहा है लेकिन जब मैं अधिक बाइट सरणी भेजना/प्राप्त करना चाहता हूं तो यह काम नहीं करता है?
यह केवल तब काम कर रहा है जब मैं शट डाउन का उपयोग करता हूं क्योंकि क्लाइंट और सर्वर दोनों डेटा पढ़ने और लिखने के लिए।
इसलिए, मैं सॉकेट चैनल का फिर से उपयोग नहीं कर सकता ... क्या वैकल्पिक समाधान है? ... जो डेडलॉक का कारण नहीं है।
पर डेटा को बदल रहा है यह काम है ... बहुत बहुत धन्यवाद। –
अच्छा जवाब, यह मेरे लिए भी काम करता है! धन्यवाद। – cabreracanal
@Javalover ... एह, क्या आप इसे काम करने के बाद वहां कोड साझा करना चाहते हैं? – gumuruh