ورود

View Full Version : سوال: چت بین دو سیستم با استفاده از جاوا...



fadanafas
شنبه 06 دی 1393, 17:01 عصر
سلام خدمت دوستان و استاتید بزرگوار

هفته قبل استادمون یه برنامه جاوا آورد سر کلاس که روی یه سیستم هم سرور درست میکرد هم کلاینت بعد هرچی تایپ میشد روی سرور نشون میداد.

آخرش گفت جوری برنامه رو تغییر بدید که روی دوتا سیستم مختلف بشه باهش چت کرد.

ممنون میشم کمکم کنید

فایلش رو هم ضمیمه کردم.

Hossein_1995
دوشنبه 15 دی 1393, 20:59 عصر
کل پروژه ر, Export کن بذار تو آرچیو ببینیم چیه

fadanafas
شنبه 20 دی 1393, 11:04 صبح
import java.io.*;
import java.net.*;
import java.util.*;
public class TCPEchoServer {
private static ServerSocket servSock;
private static final int PORT = 1234;
public static void main(String[] args) {
System.out.println("Opening port...\n");
try
{
servSock = new ServerSocket(PORT); //Step 1. ���� ���� ��̘� ���
}
catch(IOException ioEx)
{
System.out.println("Unable to attach to port!");
System.exit(1);
}
do
{
handleClient();
}while (true);
}
private static void handleClient() {
Socket link = null; //Step 2.
try
{
link = servSock.accept(); //Step 2.
Scanner input = new Scanner(link.getInputStream());//Step 3.
PrintWriter output = new PrintWriter(link.getOutputStream(),true); //Step 3.
int numMessages = 0;
String message = input.nextLine(); //Step 4.
while (!message.equals("***CLOSE***"))
{
System.out.println("Message received.");
numMessages++;
output.println("Message " + numMessages + ": " + message); //Step 4.
message = input.nextLine();
}
output.println(numMessages+ " messages received.");//Step 4. ������ ����� ����
}
catch(IOException ioEx)
{ ioEx.printStackTrace(); }
finally
{
try
{
System.out.println("\n* Closing connection... *");
link.close(); //Step 5. ����� ����
}
catch(IOException ioEx)
{
System.out.println("Unable to disconnect!");
System.exit(1);
}
}
}
}





client

import java.io.*;
import java.net.*;
import java.util.*;
public class TCPEchoClient {
private static InetAddress host;
private static final int PORT = 1234;
public static void main(String[] args) {
try
{
host = InetAddress.getLocalHost();\\اي قسمت بايد تيير داده شود
}
catch(UnknownHostException uhEx)
{
System.out.println("Host ID not found!");
System.exit(1);
}
accessServer();
}
private static void accessServer() {
Socket link = null; //Step 1.
try
{
link = new Socket(host,PORT); //Step 1.
Scanner input = new Scanner(link.getInputStream()); //Step 2.
PrintWriter output = new PrintWriter(link.getOutputStream(),true); //Step 2.
//Set up stream for keyboard entry...
Scanner userEntry = new Scanner(System.in);
String message, response;
do
{
System.out.print("Enter message: ");
message = userEntry.nextLine();
output.println(message); //Step 3.
response = input.nextLine(); //Step 3.
System.out.println("\nSERVER> "+response);
}while (!message.equals("***CLOSE***"));
}
catch(IOException ioEx)
{
ioEx.printStackTrace();
}
finally
{
try
{
System.out.println("\n* Closing connection... *");
link.close(); //Step 4.
}
catch(IOException ioEx)
{
System.out.println("Unable to disconnect!");
System.exit(1);
}
}
}
}