2 bölümden bir program yapıyorum.Soket - Java İstemcisi, C# Sunucu
Bölüm 1: C# sunucu soketi PC üzerinde çalışan, komutları dinleyen ve buna göre hareket eden bir uygulama. Bölüm 2: Telefonda çalışan Java istemcisi-soket uygulaması, bir tuşa basıldığında, bilgisayara bir komut gönderen, Java istemci soket uygulaması
Şu anda, istemciden sunucuya ve tüm iyi komutları gönderebilirim. Ama benim sorunum şudur: Sunucuya belirli bir komut gönderdiğimde, sunucunun istemciye yanıt vermesini ve istemcinin bu yanıtı okumasını istiyorum.
Sadece, istemci okumayı denediğinde, zaman aşımına uğradı.
Java istemci programı:
class ClientThread implements Runnable
{
public void run()
{
try
{
Socket socket = new Socket(serverIpAddress, serverPort);
socket.setSoTimeout(5000);
while (true)
{
try
{
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
Log.d("Nicklas", "Out it goes");
out.println(Command);
if (Command == "CMD:GetOptions<EOF>")
{
Log.d("Nicklas", "Getting options");
try
{
Log.d("Nicklas", "Line 1");
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
Log.d("Nicklas", "Line 2");
String answer = in.readLine();
Log.d("Nicklas", "answer = " + answer);
}
catch (Exception ee)
{
Log.d("Nicklasasasas", ee.toString());
}
}
break;
}
catch (Exception e)
{
Log.d("Nicklas", "CAE = " + e.toString());
break;
}
}
socket.close();
}
catch (ConnectException ee)
{
Log.d("Nicklas", "Kunne ikke forbinde");
}
catch (Exception e)
{
Log.d("Nicklasssssss", e.toString());
}
}
}
Bu çağrılır:
Thread cThread = new Thread(new ClientThread());
cThread.start();
Ve basılmış hangi tuşa bağlı olarak farklı bilgiler içerecektir küresel değişkeni "Command" kullanır.
Program "String answer = in.readline();" satırında başarısız oluyor. istisna "java.net.SocketTimeoutException" ile.
Bu
programın C# Sunucu parçasıdır:private void ListenForClients()
{
this.tcpListener.Start();
while (true)
{
//blocks until a client has connected to the server
TcpClient client = this.tcpListener.AcceptTcpClient();
//create a thread to handle communication
//with connected client
Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
clientThread.Start(client);
}
}
private void HandleClientComm(object client)
{
TcpClient tcpClient = (TcpClient)client;
NetworkStream clientStream = tcpClient.GetStream();
byte[] message = new byte[4096];
int bytesRead;
while (true)
{
bytesRead = 0;
try
{
//blocks until a client sends a message
bytesRead = clientStream.Read(message, 0, 4096);
}
catch
{
//a socket error has occured
break;
}
if (bytesRead == 0)
{
//the client has disconnected from the server
break;
}
//message has successfully been received
ASCIIEncoding encoder = new ASCIIEncoding();
//System.Diagnostics.Debug.WriteLine(encoder.GetString(message, 0, bytesRead));
string Input = (encoder.GetString(message, 0, bytesRead));
Input = Input.Trim();
object[] obj = new object[1];
obj[0] = Input;
if (Input == "CMD:GetOptions<EOF>")
{
try
{
byte[] buffer = encoder.GetBytes("CMD:Accepted");
clientStream.Write(buffer, 0, buffer.Length);
clientStream.Flush();
MessageBox.Show("Client program asked for reply");
}
catch (Exception e)
{
MessageBox.Show("Oh it no work!: " + e.ToString());
}
}
else
{
Udfor(Input);
}
}
tcpClient.Close();
}
aşağıdaki ile adlandırılan, Form1() içinde
this.tcpListener = new TcpListener(IPAddress.Any, 4532);
this.listenThread = new Thread(new ThreadStart(ListenForClients));
this.listenThread.Start();
C# Sunucusu iyi iş gibi görünüyor ve göstermektedir messagebox "istemci programı yanıt istedi"
Hata tespit edebilen herkes?
İstemcinin sunucudan yanıt almıyor gibi görünüyor mu? SocketTimeoutException, verilen zaman aşımında soket tarafından yanıt alınmadığında, örneğin 5 saniyede atılır. Sunucunun yanıtı gönderdiğinden emin misin? – Kerry
O im% 100 emin, ancak% 99 C# programı olup: halinde (Giriş == "CMD: GetOptions ") { bayt [] tamponu = encoder.GetBytes (" CMD: Kabul"); clientStream.Write (tampon, 0, buffer.Length); clientStream.Flush(); MessageBox.Show ("Ah istedim zeh seçenekleri"); } Bu, bir mesaj alındıktan sonra çalıştırılır. Ayrıca messageBox'ı da gösterir, bu yüzden yanıtı gönderdiğinden eminim –
user1285334