Witam, potrzebowałbym zrozumiec jak łączą sie ze sobą (co za co jest odpowiedzialne) patrzac na te dwa kody. Jak sa przesyłane dane jak nawiązywane jest połaczenie SwingChatClient c# i .TCPChatServer java.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Threading;
using System.IO;
namespace TCPChatServer
{
public partial class Form1 : Form
{
delegate void SetTextCallback(string text);
TcpListener listener;
TcpClient client;
NetworkStream ns;
StreamWriter sw;
StreamReader sr;
Thread t = null;
public Form1()
{
InitializeComponent();
t = new Thread(DoWork);
t.Start();
}
private void button1_Click(object sender, EventArgs e)
{
String s = textBox2.Text;
//byte[] byteTime = Encoding.ASCII.GetBytes(s);
//ns.Write(byteTime, 0, byteTime.Length);
sw.WriteLine(s); sw.Flush();
}
// This is run as a thread
public void DoWork()
{
byte[] bytes = new byte[1024];
listener = new TcpListener(4545);
listener.Start();
client = listener.AcceptTcpClient();
ns = client.GetStream();
sr = new StreamReader(ns);
sw = new StreamWriter(ns);
while (true)
{
//int bytesRead = ns.Read(bytes, 0, bytes.Length);
//this.SetText(Encoding.ASCII.GetString(bytes, 0, bytesRead));
this.SetText(sr.ReadLine());
//MessageBox.Show(Encoding.ASCII.GetString(bytes, 0, bytesRead));
}
}
private void SetText(string text)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.textBox1.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
this.textBox1.Text = this.textBox1.Text + text;
}
}
}
}
/*
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Threading;
namespace TCPChatServer
{
public partial class Form1 : Form
{
delegate void SetTextCallback(string text);
TcpListener listener;
TcpClient client;
NetworkStream ns;
Thread t = null;
public Form1()
{
InitializeComponent();
listener = new TcpListener(4545);
listener.Start();
client = listener.AcceptTcpClient();
ns = client.GetStream();
t = new Thread(DoWork);
t.Start();
}
private void button1_Click(object sender, EventArgs e)
{
String s = textBox2.Text;
byte[] byteTime = Encoding.ASCII.GetBytes(s);
ns.Write(byteTime, 0, byteTime.Length);
}
// This is run as a thread
public void DoWork()
{
byte[] bytes = new byte[1024];
while (true)
{
int bytesRead = ns.Read(bytes, 0, bytes.Length);
this.SetText(Encoding.ASCII.GetString(bytes, 0, bytesRead));
//MessageBox.Show(Encoding.ASCII.GetString(bytes, 0, bytesRead));
}
}
private void SetText(string text)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.textBox1.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
this.textBox1.Text = this.textBox1.Text + text;
}
}
}
}
*/
import java.awt.;
import java.awt.event.;
import javax.swing.;
import javax.swing.event.;
import java.io.;
import java.net.;
public class SwingChatClient extends SwingChatGUI
{
static Socket socket = null;
static PrintWriter out = null;
static BufferedReader in = null;
public ButtonHandler bHandler;
public SwingChatClient (String title)
{
super (title);
bHandler = new ButtonHandler();
sendButton.addActionListener( bHandler );
}
private class ButtonHandler implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
String outputLine;
outputLine = txArea.getText ();
System.out.println ("Client > " + outputLine);
out.println (outputLine);
}
}
public void run () throws IOException
{
try
{
socket = new Socket ("localhost", 4545);
out = new PrintWriter (socket.getOutputStream (), true);
in = new BufferedReader (new InputStreamReader (socket.getInputStream ()));
}
catch (UnknownHostException e)
{
System.err.println ("Don't know about host: 194.81.104.118.");
System.exit(1);
}
catch (IOException e)
{
System.err.println ("Couldn't get I/O for the connection to: 194.81.104.118.");
System.exit (1);
}
String fromServer;
while ((fromServer = in.readLine ()) != null)
{
System.out.println ("Client < " + fromServer);
rxArea.setText (fromServer);
if (fromServer.equals ("Bye.")) break;
}
out.close();
in.close();
socket.close();
}
public static void main(String[] args)
{
SwingChatClient f = new SwingChatClient ("Chat Client Program");
f.pack ();
f.show ();
try
{
f.run ();
}
catch (IOException e)
{
System.err.println("Couldn't get I/O for the connection to: 194.81.104.118.");
System.exit(1);
}
}
}
Dzięki z góry.