我正在做一個任務,即建立兩個java代碼通過套接字彼此進行通信。當我開始構建兩個基本代碼(一次通信)時,它運行良好,但是當我試圖讓它們保持通信時,兩者都發生了堵塞!它似乎是死鎖..兩個插座java代碼
請原諒我缺乏經驗,並幫助我..
我的服務器代碼,
/*****************************************************************************************
*
*
*
***************************************************************************************/
import java.io.*;
import java.net.*;
import javax.swing.JOptionPane;
public class COMP_3270_Server implements Runnable {
public static void main(String[] args) {
try {
ServerSocket myServer = new ServerSocket(3270);
JOptionPane.showMessageDialog(null, "A server built over " +
InetAddress.getLocalHost().getHostAddress() + " : " +
myServer.getLocalPort() + "\nWaiting for masseges...",
"succeed", JOptionPane.INFORMATION_MESSAGE);
Socket channel = myServer.accept();
BufferedReader incomes = new BufferedReader(
new InputStreamReader(channel.getInputStream()));
PrintStream outgoes = new PrintStream(channel.getOutputStream());
String mssg = null;
do {
mssg = JOptionPane.showInputDialog("New Message: " + incomes.readLine());
outgoes.print(mssg);
} while (mssg != null);
JOptionPane.showMessageDialog(null, "The server will be closed now",
"Finish", JOptionPane.INFORMATION_MESSAGE);
channel.close();
myServer.close();
} catch (IOException e) {
System.out.println("Ops!, some thing went wrong. Please contect provider");
}
}
@Override
public void run() {
// TODO Auto-generated method stub
}
}
和我的客戶端代碼,
/*************************************************************
*
*
*
* ***********************************************************/
import java.io.*;
import java.net.*;
import javax.swing.JOptionPane;
public class COMP_3270_Client implements Runnable {
public static void main(String[] args) {
try {
Socket channel = new Socket("localhost", 3270);
BufferedReader incomes = new BufferedReader(
new InputStreamReader(channel.getInputStream()));
PrintStream outgoes = new PrintStream(channel.getOutputStream());
JOptionPane.showMessageDialog(null, "Connected to: " +
channel.getInetAddress().getHostAddress() + " : " +
channel.getPort(), "succeed", JOptionPane.INFORMATION_MESSAGE);
String mssg = "New client: " + channel.getLocalAddress().getHostName();
outgoes.print(mssg);
System.out.println("222");
do {
mssg = JOptionPane.showInputDialog("New Message: " + incomes.readLine());
outgoes.print(mssg);
} while (mssg != null);
JOptionPane.showMessageDialog(null, "The channel will be closed now",
"Finish", JOptionPane.INFORMATION_MESSAGE);
channel.close();
} catch (IOException e) {
e.printStackTrace();
System.out.println("Ops!, some thing went wrong. Please contect provider");
}
}
@Override
public void run() {
// TODO Auto-generated method stub
}
}
它的幫助!!!!我幾乎完成了我的任務。 對於Socket問題,你看起來不錯!所以,我想採取一些好處;)
你有任何想法如何檢測消息是否拋出緩衝區爲空?我嘗試了幾種方法,但沒有任何工作!
如果u希望看到我的代碼後,你(也許有些人會需要它,
/*****************************************************************************************
*
***************************************************************************************/
import java.awt.HeadlessException;
import java.io.*;
import java.net.*;
import javax.swing.JOptionPane;
public class COMP_3270_Server
{
public static void main(String[] args)
{
try
{
ServerSocket myServer = new ServerSocket(3270);
JOptionPane.showMessageDialog (null, "A server built over " +
InetAddress.getLocalHost().getHostAddress() + " : " +
myServer.getLocalPort() + "\nWaiting for a client...", "succeed", JOptionPane.INFORMATION_MESSAGE);
Socket channel = myServer.accept();
BufferedReader income = new BufferedReader(new InputStreamReader(channel.getInputStream()));
PrintStream outgoes = new PrintStream(channel.getOutputStream());
String resp = " ";
String mssg = " ";
do
{
if (income.ready())
{
if(mssg.intern() == null)
{
int response = JOptionPane.showConfirmDialog(null,
"Client left sever, keep server alive?", "Client left",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if (response == JOptionPane.NO_OPTION)
{
resp = null;
}
else if (response == JOptionPane.CLOSED_OPTION)
{
resp = null;
}
else if (response == JOptionPane.YES_OPTION)
{
}
}
else
{
resp = JOptionPane.showInputDialog("New client message: " + mssg);
outgoes.println(resp);
}
}
}while (resp != null && mssg != null);
JOptionPane.showMessageDialog (null, "The server will be closed now", "Finish", JOptionPane.INFORMATION_MESSAGE);
channel.close();
myServer.close();
}
catch (HeadlessException e)
{
JOptionPane.showMessageDialog (null, "A string not supported", "Error", JOptionPane.INFORMATION_MESSAGE);
}
catch (UnknownHostException e)
{
JOptionPane.showMessageDialog (null, "IP address not available", "Error", JOptionPane.INFORMATION_MESSAGE);
}
catch (IOException e)
{
JOptionPane.showMessageDialog (null, "Failed or interrupted I/O", "Error", JOptionPane.INFORMATION_MESSAGE);
}
}
}
和
/*************************************************************
*
*
*
* References : http://www.youtube.com/watch?v=aEDV0WlwXTs
* ***********************************************************/
import java.io.*;
import java.net.*;
import javax.swing.JOptionPane;
public class COMP_3270_Client
{
public static void main (String [] args)
{
try
{
String ip = JOptionPane.showInputDialog("Enter the IP you want to connect to: ");
Socket channel = new Socket(ip, 3270);
BufferedReader income = new BufferedReader(new InputStreamReader(channel.getInputStream()));
PrintStream outgoes = new PrintStream(channel.getOutputStream());
String strt = JOptionPane.showInputDialog("Succeed! Connected. You can say something: ");
outgoes.println(strt);
String resp = " ";
String mssg = " ";
do
{
if (income.ready())
{
mssg = income.readLine();
if(mssg.intern() == null)
{
JOptionPane.showMessageDialog (null, "Server closed!", "Session end",
JOptionPane.INFORMATION_MESSAGE);
resp = null;
outgoes.println(resp);
}
else
{
resp = JOptionPane.showInputDialog("New server message: " + mssg);
outgoes.println(resp);
}
}
}while (resp != null && mssg != null);
JOptionPane.showMessageDialog (null, "The channel will be closed now", "Finish", JOptionPane.INFORMATION_MESSAGE);;
channel.close();
}
catch (UnknownHostException e)
{
JOptionPane.showMessageDialog (null, "Wrong IP address or server reject connection", "Error", JOptionPane.INFORMATION_MESSAGE);
}
catch (IOException e)
{
JOptionPane.showMessageDialog (null, "Failed or interrupted I/O", "Error", JOptionPane.INFORMATION_MESSAGE);
}
}
}
歡迎來到編程世界。第一個教訓是,你不要說'建立兩個Java代碼',而是'建立兩個Java類'或'兩個Java應用程序'或'兩個Java程序'。 – cherouvim
讓我猜 - 你的客戶端確實發送了一個消息到服務器,之後,這兩個應用程序(客戶端+服務器)終止? – home