我正在做一个任务,即建立两个通过套接字相互通信的java代码。当我开始构建两个基本代码(通信一次)它工作正常,但当我试图让他们继续沟通Jammed!
请原谅我缺乏经验并帮助我......
我的服务器代码,
/*****************************************************************************************
*
*
*
***************************************************************************************/
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问题很好!所以,我想要一些好处;)
您是否知道如何检测抛出缓冲区的消息是否为空?我尝试了几种方法,但没有任何效果!
如果你想在你之后看到我的代码(也许有人需要它,
/*****************************************************************************************
*
***************************************************************************************/
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);
}
}
}
答案 0 :(得分:0)
从套接字读取时,使用readLine
方法。它会阻塞,直到读完一行,阅读更多here。此永远不会发生,因为使用了写入套接字print
时。
在写入套接字时尝试使用println
而不是print
。
为什么你的课程实施Runnable
?这不会添加任何内容,但只会使您的代码更难阅读。
请检查您的拼写 - 它被称为“消息”而不是“masseges”。什么是“竞争提供者”?