소켓으로 서로 통신하는 두 개의 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
}
}
그것은 도왔다 !!!! 내 임무가 거의 끝났어. 소켓 문제가있는 것 같아요! 그래서, 몇 가지 이점을 싶어요;)
당신은 메시지가 버퍼가 null을 던지는 경우 감지하는 방법을 알고 있니? 몇 가지 방법을 시도했지만 아무 효과가 없습니다!
후 U 싶어 내 코드를 볼 경우 (당신이 readLine
방법을 사용하여 소켓에서 읽을 때 어쩌면 어떤 하나,
/*****************************************************************************************
*
***************************************************************************************/
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);
}
}
}
프로그래밍 세계에 오신 것을 환영합니다. 첫 번째 교훈은 '두 개의 자바 코드 빌드', '두 개의 자바 클래스 빌드'또는 두 개의 자바 응용 프로그램 또는 두 개의 자바 프로그램을 말하지 않는다는 것입니다. – cherouvim
내가 추측 해 보겠습니다. 클라이언트가 정확히 하나의 메시지를 서버에 보내면 두 앱 (클라이언트 + 서버)이 종료됩니다. – home