Java 8일차 Chat 프로그램

ChatServerTest.java

import java.net.ServerSocket;
import java.net.Socket;

public class ChatServerTest
{
public static void main(String[] args)
{
ServerSocket server = null;
Socket socket = null;

try
{
server = new ServerSocket(9000);
socket = server.accept();

ReceiveThreadTest rt = new ReceiveThreadTest(socket);

SendThreadTest st = new SendThreadTest(socket);

rt.start();
st.start();
}
catch(Exception e)
{
}
}
}

ChatClientTest.java

import java.net.Socket;

public class ChatClientTest
{
public static void main(String[] args)
{
Socket socket = null;
try
{
socket = new Socket("203.233.196.159", 9000);
ReceiveThreadTest rt = new ReceiveThreadTest(socket);
SendThreadTest st = new SendThreadTest(socket);
rt.start();
st.start();
}
catch(Exception e)
{
}
}
}

SendThreadTest.java

import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class SendThreadTest extends Thread
{
Socket socket;
public SendThreadTest(Socket socket)
{
this.socket = socket;
}
public void run()
{
Scanner sc = new Scanner(System.in);
try
{
PrintWriter pw = new PrintWriter(socket.getOutputStream());
while(true)
{
String msg = sc.nextLine();
if(msg.equals(""))
break;
pw.write("송신 : "+msg);
pw.flush();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

RecieveThreadTest.java

import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class ReceiveThreadTest extends Thread
{
Socket socket;
public ReceiveThreadTest(Socket socket)
{
this.socket = socket;
}
public void run()
{
try
{
Scanner sc = new Scanner(socket.getInputStream());
while(true)
{
String msg = sc.nextLine();
if(msg.equals(""))
break;
System.out.println("수신 : "+msg);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}



댓글 없음: