UDP PacketÀ» ¹ÞÀ» ¼ö°¡ ¾ø¾î¿ä..±×¸®°í, ¾²·¹µå ³¢¸® µ¥ÀÌÅ͸¦ °¡Á®°¡´ÂÁö ¸ð¸£°Ú¾î¿ä...


[ ´ÙÀ½ ±Ûµé ] [ À̾ ±Û¿Ã¸®±â(´äÇϱâ) ] [ ÀÚ¹Ù ¹¯°í ´äÇϱâ ]

±Û¾´ÀÌ :±è¿ëŹ 2000³â 7¿ù 05ÀÏ 03:58:44

UDP·Î µ¥ÀÌÅ͸¦ ¹Þ¾Æ¼­, TCP·Î Á¢¼ÓÇØ ¿À´Â Userµé¿¡°Ô


µ¥ÀÌÅ͸¦ º¸³»ÁÖ´Â ApplicationÀ» Á¦ÀÛÇÏ°í ÀÖ½À´Ï´Ù.


±×·±µ¥, UDP·Î µ¥ÀÌÅÍ PacketÀÌ ¹Þ¾ÆÁöÁö ¾Ê´Â°Å¿¹¿ä...


¾î¶»ÇÏÁÒ?


¶Ç, UDP ¾²·¹µå·Î µ¥ÀÌÅ͸¦ ¹Þ´Â´Ù Çصµ,


TCP Server¿¡ ÇØ´çÇÏ´Â ¾²·¹µå·Î µ¥ÀÌÅ͸¦ ¹Þ°Ô Çϴ°Ô


¾î¶»°Ô Çغ¸±ä ÇßÁö¸¸ µÇ´ÂÁö ¾ÈµÇ´Â ¸ð¸£°Ú¾î¿ä...


Á¤È®ÇÑ ¹æ¹ýÀÌ ¾Æ´Ñ °Í °°°Åµç¿ä...


°í¼ö´Ôµé...Á¦¹ß..Á¦¹ß...¹æ¹ý Á» ¾Ë·ÁÁּſä...ÈæÈæ


¼Ò½º¸¦ ÇÔ²² ¿Ã¸³´Ï´Ù.



import java.net.*;
import java.io.*;
import java.lang.*;


public class RealTimeServ implements Runnable
{
static int users = 0;
static int tcpPort = 7965;
static String tKospi;
static String tKospi200;
static String tKosdaq;
static socket_stuff clients[] = new socket_stuff[300];


public static void main(String args[])
{
RealTimeServ rt = new RealTimeServ();
Thread t = new Thread(rt);
t.start();

}

public void run()
{
ServerSocket serverSocket = null;


try
{
serverSocket = new ServerSocket(tcpPort);
}
catch (IOException e)
{
System.out.println("Could not listen on port: " + tcpPort + "," + e);
System.exit(1);
}

Socket clientSocket = null;
connection user = new connection(serverSocket);

if (users == 0)
{
try
{
clientSocket = serverSocket.accept();
}
catch (IOException e)
{
System.out.println("Accept failed: "+ tcpPort + "," + e);
System.exit(1);
}
add_client(new socket_stuff(clientSocket));
}

UDPPacket udp = new UDPPacket();
udp.start();

user.start();

int i;


try
{
i = users;

while (users >= 0)
{
tKospi = udp.pKospi; // <----- ¿©±â¼­ UDP µ¥ÀÌÅ͸¦ TCP·Î ³Ñ°ÜÁØ´Ù°í »ý°¢ÇÏ°í ¸¸µé¾ú½À´Ï´Ù..¸Â³ª¿ä?
tKospi200 = udp.pKospi200;
tKosdaq = udp.pKosdaq;

if (clients[i] != null)
write_all(tKospi+tKospi200+tKosdaq);
try
{
user.join(2);
}
catch (InterruptedException e) {}

if (user.connected() == true)
{
add_client(new socket_stuff(user.get_socket()));
user.resume();
}

i++;


if(i>users && users>=1)
i=1;


if(i>users && users<1)
i=0;


try
{
Thread.sleep(3000);
}
catch (InterruptedException e)
{}
}


serverSocket.close();
} catch (IOException e) {}
}


static void write_all(String text)
{
int i;
for(i=1;i<=users;i++)
clients[i].write_string(text);
}


static void add_client(socket_stuff client)
{
if (users >= 300)
{
client.kill();
return;
}


clients[++users] = client;
}


static void remove_client(int client)
{
clients[client].kill();
clients[client] = clients[users];
clients[users]=null;
users--;
}
}



class connection extends Thread
{
Socket socket = null;
public boolean is_connected = false;
ServerSocket server = null;


connection(ServerSocket server_sock)
{
server = server_sock;
}


boolean connected()
{
return is_connected;
}


Socket get_socket()
{
is_connected = false;
return socket;
}


public void run()
{
while (true)
{
is_connected = false;


if (!is_connected)
{
try
{
socket = server.accept();
} catch(IOException e)
{
System.out.println("Problem with accept");
}


is_connected = true;
this.suspend();
}
}
}
}


class socket_stuff
{
Socket socket;
PrintWriter os;


socket_stuff(Socket client)
{
try
{
os = new PrintWriter(new BufferedOutputStream(client.getOutputStream(),1024),false);
socket = client;
} catch(IOException e) {}
}



public void write_string(String s)
{
os.println(s);
os.flush();
}


public void kill()
{
try
{
if (os != null)
os.close();

if (socket!= null)
socket.close();
} catch (IOException e) {}
}
}


class UDPPacket extends Thread // <--- ¿©±â°¡ UDP PacketÀ» ¹Þ´Â Class Àε¥...¾ÈµÇ¿ä..ÈæÈæÈæ
{
public static String pKospi;
public static String pKospi200;
public static String pKosdaq;

private static int Port = 7859;
private DatagramSocket ds;
private DatagramPacket dp;

public void run()
{
byte[] buffer = new byte[1024];


String line, result;
StringBuffer revline;
int len;


try
{
ds = new DatagramSocket(Port);


for(;;)
{
dp = new DatagramPacket(buffer, buffer.length);
try
{
ds.receive(dp);
line = new String(dp.getData(), 0, 0, dp.getLength());
System.out.println("line: " + line);
len = line.length();
System.out.println("the length of this string :"+len);


redirectPacket(line);
line = null;


try
{
sleep(300);
}
catch (InterruptedException e) {}
}
catch (IOException e)
{
System.err.println(e);
}
}
}
catch (SocketException se)
{
System.err.println(se);
}
}


public static void redirectPacket(String str)
{
String kospi = "I1";
String kospi200 = "I2";
String kosdaq = "OJ";
String head = "01";


int length;
String conhead, conkind;


conhead = str.substring(1, 3);
conkind = str.substring(3, 5);


if (conhead.compareTo(kospi) == 0)
{
if (conkind.compareTo(head) == 0)
{
pKospi = str.substring(12, 18);
System.out.println("pKospi: " + pKospi);
}
}


if (conhead.compareTo(kospi200) == 0)
{
if (conkind.compareTo(head) == 0)
{
pKospi200 = str.substring(12, 18);
System.out.println("pKospi200: " + pKospi200);
}
}


if (conhead.compareTo(kosdaq) == 0)
{
if (conkind.compareTo(head) == 0)
{
pKosdaq = str.substring(10, 16);
System.out.println("pKosdaq: " + pKosdaq);
}
}
}
}


´ÙÀ½ ±Ûµé:



À̾ ±Û¿Ã¸®±â(´äÇϱâ)

À̸§:
E-Mail:
Á¦¸ñ:
³»¿ë:
HTML ÅÂ±× Æ÷ÇÔ ¿©ºÎ: HTML ¹®¼­ÀÏ °æ¿ì üũ
°ü·Ã URL(¼±ÅÃ):
URL Á¦¸ñ(¼±ÅÃ):
°ü·Ã À̹ÌÁö URL:


[ ´ÙÀ½ ±Ûµé ] [ À̾ ±Û¿Ã¸®±â(´äÇϱâ) ] [ ÀÚ¹Ù ¹¯°í ´äÇϱâ ]