You are on page 1of 21

Tm hiu v lp trnh socket trong java I, Socket 1. Socket: Mt socket l mt im kt ni truyn thng (u cui) m bn cn 1 tn(name) v a ch(address) trn mng.

Socket c th s dng trn cc h thng ging nhau hoc cc h thng khc nhau trn cc mng khc nhau. Socket hu dng cho c ng dng c lp v ng dng mng. Socket cho php bn thay i thng tin gia vic x l cc trn cc my ging nhau v hai bn u cui trn mt mng va cho php truy xut mt cch d dng ti cc d liu trung tm. Socket application program interfaces (APIs): l mt mng chun TCP/IP. Phn ln cc h iu hnh u h tr APIs. OS/400 socket h tr a truyn ti v cc giao thc mng. Cc hm socket system v socket network c h tr bo mt. Mt chng trnh Socket ch cho ta thy lm th no s dng Socket APIs thit lp lin kt truyn thng gia cch thc truy xut t xa v cc b. Programers l nhng ngi s dng Integrated Language Environment (ILE) (dch tm l mi trng hp ng ) pht trin cc ng dng Socket. Bn c th lp trnh Socket thng qua nhiu ngn ng lp trnh (ILE) khc nhau. Java l mt trong nhng ngn ng lp trnh dng lp trnh cc ng dng Socket. Java cn h tr giao din (Socket programing interface). 2. Socket lm vic nh th no? Sockets phn ln c s dng cho vic truyn thng gia client v server. in hnh l cu hnh server trn 1 my v clients trn 1 my khc. Clients kt ni ti server, trao i thng tin v sau kt thc vic truyn thng, chng s ngt kt ni. Mt socket c mt lot cc kh nng. Trong mt kt ni kiu client-to-server, Socket trn server s i x l cc yu cu t mt client lm iu ny, server u tin phi thit lp mt a ch m cc clients da vo tm server. Khi a ch server c thit lp, server s i cho cc clients gi yu cu v 1 dch v. D liu lc s c trao i thng qua socket. Server thc hin yu cu t cc Client v gi phn hi li cho Client. Hnh v b miu t mt phin kt ni Socket.

Manlivo at3akma http://blog.sucmanhtinhoc.com

Tm hiu v lp trnh socket trong java

Bc 1: Hm socket() to mt im kt ni (endpoint u cui) cho vic kt ni v tr li mt k hiu nhn din socket tng ng vi im kt ni (endpoint). Bc 2: Khi mt ng dung c mt k hiu nhn din socket, n c th gn 1 tn duy nht cho socket. Server phi gn 1 tn c th truy xut ti mng. Bc 3: Hm listen() cho thy s sn sng chp nhn yu cu kt ni t pha client. Bc 4: ng dng Client s dng hm connect() trn mt stream socket thc hin kt ni ti server. Bc 5: ng dng server s dng hm accept() chp nhn yu cu kt ni t 1 client. Server phi thc hin hm bind() v listen() thnh cng trc khi n c th thc hin hm accept(). Bc 6: Khi mt kt ni c thc hin gia Client v Server, chng ta c th dng mt s hm truyn d liu Socket API. Cc Client v Server c nhiu hm truyn d liu la chn nh send(), recv(), read(), write()... Bc 7: Khi Server hoc Client mun ngng vic truyn thng, n thc hin hm close() gii phng ti nguyn m h thng b ra cho socket. Note: Socket APIs c xc inh trong m hnh truyn thng gia lp ng dng v tng vn chuyn. Socket APIs khng phi mt tng ca m hnh truyn thng. Socket APIs cho php ng dng tc ng vi tng vn chuyn v tng mng ca mt m hnh truyn thng din hnh. Mi tn trong hnh sau y ch ra v tr ca mt socket v lp truyn thng m socket cung cp.

in hnh, mt cu hnh mng khng cho php kt ni gia mt mng bo mt trong v mt mng bo mt thp bn ngoi. Tuy nhin bn c th cho php truyn thng gia chng trnh ca server chy trn mt h thng ngoi firewall. 3. Socket characteristics

Manlivo at3akma http://blog.sucmanhtinhoc.com

Tm hiu v lp trnh socket trong java

Khi mt ng dng to 1 socket vi hm socket(), n phi nh dng socket vi 3 tham s Socket adddress family l mt dng ca cu trc a ch ca socket. Socket type xc nh form ca truyn thng ca socket Socket-supported protocols: giao thc m socket s dng

Bng tm tc socket characteristics Address family AF_UNIX AF_INET Socket type SOCK_STREAM SOCK_DGRAM SOCK_STREAM SOCK_DGRAM SOCK_RAW SOCK_STREAM SOCK_DGRAM SOCK_RAW SOCK_STREAM SOCK_STREAM SOCK_DGRAM Socket protocol N/A N/A TCP UDP IP,ICMP TCP UDP IP6, ICMP6 N/A N/A N/A

AF_INET6

AF_TELEPHONY

SOCKET address family: Tham s a ch address family trn mt socket() l dng ca cu trc a ch s dng trn hm socket. Giao thc address family cung cp s truyn cc d liu ng dng t mt ng dng ti 1 ng dng khc. Cc a ch address family AF_INET address family AF_INET6 address family AF_UNIX address family AF_UNIX_CCSID address family AF_TELEPHONY address family

II, Cc loi socket 1. Phn loi Tham s th 2 trn mt socket quyt nh loi Socket. Socket cung cp s nhn din loi v c im m s thc hin vic chuyn d liu t my ny sang my khc. Danh sch miu t cc loi socket m iSeries h tr: a. Stream(SOCK_STREAM) Loi Socket ny l mt kt ni nh hng. Thit lp mt kt ni im ni im bng vic s dng hm bind(), listen(), accept(), v connect(). SOCKET_STREAM gi Manlivo at3akma http://blog.sucmanhtinhoc.com

Tm hiu v lp trnh socket trong java d liu v nhn d liu trong mt danh sch. SOCK_TREAM xem xt d liu tr thnh cc lun cc byte. Trong mt iSeries implementation, bn c th s dng nhiu lung socket qua TCP (Hay cn gi l TCP socket) b. Datagram(SOCK_TREAM) Trong thut ng Internet Protocol, n v c bn ca d liu l datagram. Datagram Socket l kt ni thp. N thit lp kt ni khng phi im vi im v vi giao thc. Socket gi cc datagramnh cc gi tin c lp m khng cn s dm bo hay chuyn tip. Bn c th b mt hoc b sao chp d liu. D liu c th ti m ko theo mt th t no. Kch c ca cc datagram b gii bn bi kch c ca d liu. Bn c th s dng hm connect() trn loi socket ny. Tuy nhin hm connect(), bn phi ch r a ch ch m chng trnh c th gi v nhn d liu t n. Trong iSeries implementation, bn c th s dng qua UDP. (Hay cn gi l UDP socket) c. Raw (SOCK_RAW) Loi socket ny cho php truy xut ti cc giao thc tng di, nh giao thc IP (Ipv4, Ipv6) v ICMP(ICMP hoc ICMP6). SOCK_RAW yu cu lp trnh thnh tho bi v bn qun l thng tin protocol header c s dng bi transport provider. 2, Socket protocols Cc giao thc cung cp vic vn chuyn cc d liu ca ng dng trn mng t 1 my n 1 my khc. Cc ng dng ch r giao thc trn tng transport vi cc tham s giao thc ca hm socket(). III. Ni dung ca socket Asynchronous I/O: ng b vo ra. Secure sockets: Bo mt socket Client SOCKS support: H tr Client SOCKS Thread safety Non-blocking I/O Signals: Cc tn hiu IP multicasting File data transfer send_file() and accept_and_recv() Out-of-band data I/O multiplexing-select() Socket network functions Domain name system(DNS) support BSD compatibility Pass descriptors between processes-sendmsg() and recvmsg() 1. AsynchronousI/O (ng b vo ra)

Manlivo at3akma http://blog.sucmanhtinhoc.com

Tm hiu v lp trnh socket trong java ng b vo ra APIs cung cp cc phng thc cho cc m hnh client-server to s tng ng gia client v server v hiu sut vo ra b nh cao nht. Trong m hnh Client/Server, in hnh l hai m hnh vo ra. Mt l mt lung c 1 client kt ni. M hnh ny tiu tn qu nhiu lung v v lm cho cc trng thi khng c s dng mt cch hiu qu v y chi ph ln cao. Hai l ti thiu s lng cc lung bng vic dng hm select() cp cho mi mt client kt ni v y quyn cho client sn sng kt ni hoc yu cu mt lung. Trong m hnh th 2 bn phi la chn hay nh du cho mi la chn tip theo, m c th nguyn nhn ca lng ln d tha cng vic. Asynchronouns I/O advantages S dng ti nguyn h thng hiu qu. D liu c sao chp t ngi dng l s ng b t ng dng khi bt u yu cu. Ti thiu ha x l, lung v thi gian i cung cp ng dng ngay lp tc khi client yu cu Ch ra li ca ng dng Cung cp phn ln cc phng thc hiu qu trong vic x l lng ln cc d liu di chuyn. Ti thiu s lung cn s dng. Ty chn thi gian ch ra thi gian ti a cho php hon tt mt ng b. Server s ng mt kt ni Client nu n khng c hot ng trong khong thi gian c t trc. ng b thi gian s cho php server thit lp gii hn thi gian. ng b phin bo mt vi hm gsk_secure_soc_startInit() API Bng ng b I/O APIs Function gsk_secure_soc_startInit() Description Starts an asynchronous negotiation of a secure session, using the attributes set for the SSL environment and the secure session. Note: This API only supports sockets with address family AF_INET or AF_INET6 and type SOCK_STREAM. Starts an asynchronous receive operation on a secure session. Note: This API only supports sockets with address family AF_INET or AF_INET6 and type SOCK_STREAM.

gsk_secure_soc_startRecv()

Manlivo at3akma http://blog.sucmanhtinhoc.com

Tm hiu v lp trnh socket trong java gsk_secure_soc_startSend() Start an asynchronous send operation on a secure session. Note: This API only supports sockets with address family AF_INET or AF_INET6 and type SOCK_STREAM. QsoCreateIOCompletionPort() Create a common wait point for completed asynchronous overlapped I/O operations. The QsoCreateIOCompletionPort() function returns a port handle that represents the wait point. This handle is specified on the QsoStartRecv(), QsoStartSend(), QsoStartAccept(), gsk_secure_soc_startRecv(), or gsk_secure_soc_startSend() functions to initiate asynchronous overlapped I/O operations. Also this handle can be used with QsoPostIOCompletion() to post an event on the associated I/O completion port. Destroys an I/O completion port. Waits for completed overlapped I/O operation. The I/O completion port represents this wait point. Starts an asynchronous accept operation. Note: This API only supports sockets with address family AF_INET or AF_INET6 and type SOCK_STREAM. Starts an asynchronous receive operation. Note: This API only supports sockets with address family AF_INET or AF_INET6 and type SOCK_STREAM. QsoStartSend() Starts an asynchronous send operation. Note: This API only supports sockets with the AF_INET or AF_INET6 address families with the SOCK_STREAM socket type. QsoPostIOCompletion() Allows an application to notify a completion port that some function or activity has occurred.

QsoDestroyIOCompletionPort() QsoWaitForIOCompletionPort()

QsoStartAccept()

QsoStartRecv()

Manlivo at3akma http://blog.sucmanhtinhoc.com

Tm hiu v lp trnh socket trong java ng b vo ra lm vic th no? Mt ng dng s to mt cng vo ra s dng hm QsoCreateIOCompletionPort() API. API s tr v mt phng thc m c th s dng ln k hoch v i mt mt yu cu ng b vo ra. ng dng bt u mt hm input v outputt, ch ra mt cng vo ra. Khi vo ra I/O hon tt, thng tin trng thi v mt phng thc ng dng nh s s gi ti cng vo ra, v s khi ng mt trong nhng cng ang i. ng dng s nhn: 2. Mt vng m c cung cp bi yu cu gc. D liu s c x l ti v t vng m. Mt ch s ca loi iu khin vo ra m c hon thnh. Secure sockets

Hin ti, OS/400 h tr 2 phng thc ca bo mt ng dng socket trn iSeries. l SSL_APIs v Global Secure Toolkit (GSKit) APIs cung cp kh nng truyn thng ring t qua mt mng truyn thng m nh internet. APIs cho php ng dng client/server truyn trn mt ng m c thit k ngn chn s can thip, xm nhp v gi mo thng ip.C hai u h tr xc xc thc cho c server v client v cho php s dng giao thc SSL (Secure Sockets Layer). Tuy nhin, Gskit APIs h tr v l nn tng cho cc my IBM server. Khi qut chung v bo mt socket: Khi u c pht trin bi Netscape, Giao thc secure Socket Layer (SSL) l giao thc c s dng trn mt cung cp s chuyn pht tin cy nh TCP cung cp kh nng truyn thng tin cy cho mt ng dng. Mt vi ng dng yu truyn thng tin cy nh HTTPs, FTPs, SMTP v TELNETs. Khi mt ng dng c t trong ch SSL th n thng xuyn cn s dng mt cng khc so vi ng dng khng tch hp SSL. Mt trnh duyt tch hp SSL truy xut mt server HTTP tch hp SSL v 1 ng dn URL nh HTTPs. Trong phn ln cc trng hp, mt URL ca HTTPs c gng m mt kt ni cng 443 ca h thng server thay v cng 80 ca mt server HTTP chun s dng. C rt nhiu phin bn ca giao thc SSL c nh ngha. V phin bn mi nht l TLS (Transport Layer Security) version 1.0 cung cp s thay i nng cp t SSL version 3.0. C iSerises-native SSL_APIs v GSKit API h tr TLS version 1.0, TLS version 1.0 tng thch vi SSL version 3.0, SSL version 3.0 tng thch vi SSL version 2.0. Global Secure ToolKit (GSKit) APIs GSKit l mt tp hp giao din chng trnh c th lp trnh c cho php mt ng dng tch hp SSL. Ging nh SSL_APIs, GSKit APIs cho php ban c th truy xut ti hm SSL v TLS t chng trnh ng dng socket. Ngoi ra, mt GSKit API mi c

Manlivo at3akma http://blog.sucmanhtinhoc.com

Tm hiu v lp trnh socket trong java th thm to mi mt ng b trng hp ca 1 phin bo mt socket. API cung cp kt ni tin cy cho nhiu client hoc nu s yu n cao v v yu cu nhiu cng vic. Ch : APIs ch h tr socket vi a ch address family ca _INET hoc AF_INET6 v loi SOCK_STREAM (TCP) Bng sau miu t cc hm Function gsk_attribute_get_buffer() Description Obtains specific character string information about a secure session or an SSL environment, such as certificate store file, certificate store password, application ID, and ciphers. Obtains specific information about either the server or client certificate for a secure session or an SSL environment. Obtains specific information about either the server or client certificate for a secure session or an SSL environment. Obtains values for specific enumerated data for a secure session or an SSL environment. Obtains specific numeric information about a secure session or an SSL environment. Sets a specified buffer attribute to a value inside the specified secure session or an SSL environment. Sets a specified enumerated type attribute to an enumerated value in the secure session or SSL environment. Sets specific numeric information for a secure session or an SSL environment. Closes the SSL environment and releases all storage associated with the environment. Initializes the SSL environment after any required attributes are set. Returns an SSL environment handle that must be saved and used on subsequent gsk calls. Closes a secure session and free all the associated resources for that secure session. Negotiates a secure session, using the attributes set for the SSL environment and the secure session. Performs miscellaneous functions for a

gsk_attribute_get_cert_info()

gsk_attribute_get_enum_value()

gsk_attribute_get_enum_value()

gsk_attribute_get_numeric_value() gsk_attribute_set_buffer()

gsk_attribute_set_enum()

gsk_attribute_set_numeric_value() gsk_environment_close() gsk_environment_init() gsk_environment_open()

gsk_secure_soc_close() gsk_secure_soc_init()

gsk_secure_soc_misc()

Manlivo at3akma http://blog.sucmanhtinhoc.com

Tm hiu v lp trnh socket trong java secure session. Obtains storage for a secure session, sets default values for attributes, and returns a handle that must be saved and used on secure session-related function calls. Receives data from a secure session. Starts an asynchronous negotiation of a secure session, using the attributes set for the SSL environment and the secure session. Writes data on a secure session. Initiates an asynchronous receive operation on a secure session. Initiates an asynchronous send operation on a secure session. Retrieves an error message and associated text string which describes a return value that was returned from calling a GSK API.

gsk_secure_soc_open()

gsk_secure_soc_read() gsk_secure_soc_startInit()

gsk_secure_soc_write() gsk_secure_soc_startRecv() gsk_secure_soc_startSend() gsk_strerror()

Mt ng dng s dng GSKit APIs bao gm: Mt hm socket() dng nhn din socket Mt hm gsk_environment_open() Mt hoc nhiu hm gsk_attribute_set_xxxxx() lp thuc tnh cho SSL environment - Mt hm gsk_environment_init() khi to environment cho x l processing v thip lp thng tin bo mt SSL cho tt c cc phin SSL chy s dng environment - Socket s dng hm connect() kich hot kt ni cho mt chng trnh client hoc s dng bind(), listen() v accept() chp nhn nhn yu cu kt ni. - Mt hm gsk_secure_soc_open() khi ng phin bo mt - Mt hoc nhiu hm gsk_attribute_set_xxxxx() lp thuc tnh ca cc phin bo mt.Nh hm gsk_attribute_set_numeric_value() kt hp mt socket ring bit vi phin bo mt. .. SSL_APIs SSL_APIs cung cp v cho php cc lp trnh vin to ng dng bo mt socket trn iSeries. Khng ging nh GSKit_APIs, SSL_APIs ch c tch hp ti h thng OS/400. Bng sau miu t 9 SSL_APIs h tr b sung cho OS/400. Function SSL_Create() SSL_Destroy() Description Enable SSL support for the specified socket descriptor. End SSL support for the specified SSL

Manlivo at3akma http://blog.sucmanhtinhoc.com

Tm hiu v lp trnh socket trong java session and socket. Initiate the SSL handshake protocol. Initialize the current job for SSL and establish the SSL security information for the current job. Note: Either an SSL_Init() or SSL_Init_Application() API must be executed in a process before SSL can be used. Initialize the current job for SSL and establish the SSL security information for the current job. Note: Either an SSL_Init() or SSL_Init_Application() API must be executed in a process before SSL can be used. Receive data from an SSL-enabled socket descriptor. Write data to an SSL-enabled socket descriptor. Retrieve SSL runtime error message. Print SSL error message.

SSL_Handshake() SSL_Init()

SSL_Init_Application()

SSL_Read() SSL_Write() SSL_Strerror() SSL_Perror()

3. Client SOCKs support iSeries s dng SOCKs version 4 cho thc thi cc chng trnh s dng a ch AF_INET address family vi loi socket SOCK_STREAM truyn vi 1 cc chng trnh ng vai tr nh server chy trn cc h thng ko chu nh hng ca firewall. Mt firewall l mt host bo mt m ngi qun tr mng t gia mt mng bo mt ni b v mt mng ngoi km bo mt. in hnh l cu hnh mng khng cho php truyn thng bt u t host bo mt tr ti mng bo mt km hn. Mt sever Proxy tn ti trn firewall gip qun l cc yu cu truy xut gia host bo mt v mng km bo mt. ng dng chy tren cc host trong mng bo mt ni b phi gi cc yu cu ti firewall proxy server tm ng i trn firewall. Proxy server c th chuyn tip yu cu ti server tht trn mng bo mt km hn v tr li li cho ng dng trn cc host u tin. Mt vd ph bin ca mt proxy server l HTTP proxy server. Proxy server thc hin mt s cc cng vic cho cc HTTP client: - Chng giu mng ni b ca bn khi cc h thng bn ngoi - Chng bo v cc host khi cc truy cp trc tip t bn ngoi - Chng c th lc d liu ti t bn ngoi nu chng c cu hnh ng HTTP proxy server ch cho cc HTTP client

Manlivo at3akma http://blog.sucmanhtinhoc.com

10

Tm hiu v lp trnh socket trong java Mt s la chn ph bin chy nhiu proxy server trn mt firewall l SOCKs server server porxy mnh. Mt SOCKs server c th tc ng nh 1 proxy cho cc kt ni TCP client, c thit lp s dng socket API. Li th kha c h tr cho iSeries Client SOCKs m n khi ng ng dng client truy xut ti mt SOCKS server trong sut m khng lm thay i client code. Hnh v minh ha bn di ch ra mt firewall ph bin c sp t mt HTTP proxy, mt telnet proxy v mt SOCKs proxy trn 1 firewall. Ch rng 2 kt ni TCP ring r s dng cho bo mt client truy xut ti mt server trn internet. Mt kt ni chnh t mt host bo mt ti server SOCKS v cc ci khc t mng bo mt km hn ti SOCKs server.

Hai tc ng c yu cu trn secure client host s dng SOCKS server: 1. Cu hnh ca mt SOCKS server. Ngy 15/3/200 IBM thng bo rng IBM firewall cho sn phm iSeries, cung cp h tr SOCKs server s khng c thm cc tnh nng so vi V4R4 hin ti. 2. Trn mt h thng bo mt client, nh r tt c kt ni TCP client ra bn ngoi s c tr trc tip ti SOCKS server trn h thng Client. Bn c th cu hnh bo mt client SOCKs bng vic s dng SOCKS tab tm thy di hm iSeries Navigator ca iSeries Access 95 hoc Window NT.

Manlivo at3akma http://blog.sucmanhtinhoc.com

11

Tm hiu v lp trnh socket trong java

Ch : D liu cu hnh bo mt client SOCKS s dc lu trong file QASOSCFG trong th vin QUSRSYS trn h thng bo mt client host. 4. Thread safety (an ton lun) Mt hm l considered thread safety nu bn c th khi ng n ng thi trong nhiu lung khc nhau trong khong thi gian s l ging nhau. Mt hm l threadsafe nu v ch nu tt c cc hm n cn gi l threadsafe. Socket APIs gm cc hm h thng v mng. Chng u l an ton lun (thread safe). Tt c cc hm mng vi tn kt thc bi _r. Mt cng c phn tch th tc khc l threadsafe tuy khc nhau nhng chng u s dng cu trc d liu _res. Cu trc d liu l chia s gia tt c cc lung trong mt x l v c th thay i bi chng trnh ng khi mt cng c phn tch c gi. 5. Non-blocking I/O Khi mt ng dng to ra mt hm socket input, v khng c d liu no c c, hm s b ng, v trng c tr v cho ti khi c d liu c. Tng t, mt ng dng c th ng trn mt hm socket output khi d liu khng th gi ngay lp tc. Cui cng, connect() v accept() c th b ng khi i cho vic thit lp kt ni vi chng trnh. Sockets cung cp mt phng thc cho vic khi to chng trnh ng dng to ra cc hm block v vy hm tr v m khng c tr. Hon tt bng gi fcntl() bt c O_NONBLOCK, hoc gi ioctl() bt FIONBIO flag. Nu mt hm khng th hon tt m khng ng, n s tr li ngay lp tc. Mt connect() phi tr li vi [EINPROGRESS], ngha l kt ni c bt u. Bn c th s dng hm select() xc nh khi no kt ni c hon tt. Tt c cc hm nh hng bi m hnh nonblocking, mt m li ca [EWOULDBLOCK] ch ra rng gi thnh cng. Bn c th s dng non-blocking vi cc hm socket di y: accept() connect() gsk_secure_soc_read() gsk_secure_soc_write()

cu hnh h tr client SOCKS cn: Trong iSeries Navigator, m rng iSeries server -> Network -> TCP/IP Configuration Kch chut phi TCP/IP Configuration Click Properties Click ln thanh tab SOCKS in kt ni thng tin kt ni trn trang SOCKS

Manlivo at3akma http://blog.sucmanhtinhoc.com

12

Tm hiu v lp trnh socket trong java read() readv() recv() recvfrom() recvmsg() send() send_file() send_file64() sendmsg() sendto() SSL_Read() SSL_Write() write() writev()

6. Signal (tn hiu) Mt chng trnh ng dng yu cu phi thng bo ng b (yu cu h thng gi 1 tn hiu). Khi iu kin ng dng m ta quan tm tm thy. l 2 tn hiu ng b m socket s gi ti ng dng. a. SIGURG l mt ng dng s c gi khi d liu OOB (out-of-band) c nhn trn mt socket m ni dung bn trong n c h tr. VD, mt socket vi da ch address family ca AF_INET v loi SOCK_STREAM c iu kin gi tn hiu SIGURG. SIGIO l mt tn hiu m s c gi khi d liu normal, d liu OOB, trng thi li hoc c mt vn xy ra trn trn bt k loi socket no.

b.

ng dng bo m rng n s nhn tn hiu trc khi n yu cu h thng gi tn hiu. Hoi thnh bi ci t signal handlers. Mt cch thit lp mt signal handler bng vic gi hm sigaction() Mt ng dng yu cu h thng gi tn hiu SIGURG bng cc phng thc sau y: - Gi ra hm fcntl() v ch r mt ID x l hoc mt nhm ID x l vi dng lnh F_SETOWN. - Gi ra hm ioctl() v ch ra dng lnh FIOSETOWN hoc SIOCSPGRP (yu cu) Mt ng dng yu cu h thng gi tn hiu SIGIO qua 2 bc. u tin n phi thit lp ID x l hoc nhm ID x l nh miu t trn cho tin hiu SIGURG. l a ra h thng m ng dng mun tn hiu c chuyn tip. Th 2, ng dng phi lm cng vic sau y Gi hm fcntl() v ch r dng lnh F_SETFL vi c FASYNC Gi hm ioctl() v ch r dng lnh FIOASYNC

Manlivo at3akma http://blog.sucmanhtinhoc.com

13

Tm hiu v lp trnh socket trong java Bc ny yu cu h thng to ra tn hiu SIGIO. Ch cc bc c th hon bt k th t no. Cn ch rng mt ng dng pht ra nhiu yu cu trn mt socket lng nghe., gi tr c thit lp bi yu cu tha k bi tt c cc socket m tr v ti ng dng t hm accept(). Cac socket chp nhn gn nht s cn c ID x l ging nhau hoc nhm ID x l c thng tin ging nhau. Mt socket cn c th to tn hiu ng b ngay trong trang thi li. Bt c lc no mt ng dng nhn [EPIPE] mt errono trn hm socket, mt tn hiu SIGPIPE c chuyn tip ti qu trnh m pht gi tr errno. Khi mt tn hiu chuyn tip ti mt qu tnh m b kha bi hm socket, hm tr v t lc i vi gi tr [EINTR] errno, cho php ng dng chy. Cc hm cn thit l: - accept() - connect() - read() - readv() - recv() - recvfrom() - recvmsg() - select() - send() - sendto() - sendmsg() - write() - writev() 7. IP multicasting (truyn ti nhiu IP) IP multicasting cho php 1 ng dng gi ti 1 IP datagram m 1 nhm cc host trong 1 mng c th nhn c. Cc host ny trong 1 nhm phi cng 1 subnet hoc trn cc subnet khc nhau nhng cc router c th kt ni c. Cc host phi tham gia v ri cc nhm bt k thi im no. Khng hn ch trn mng cc b hoc s lng thnh vin tham gia vo nhm. a ch lp D c phm vi 224.0.0.1 ti 239.255.255.255 dng cho 1 nhm host. Bn ch c th s dng IP multicasting vi a ch AF_INET address family Mt chng trnh ng dng c th gi hoc nhn cc datagram s dng socket API, loi socket SOC_DGRAM. Multicasting l mt trong nhiu phng thc truyn. Kt ni nh hng socket ca loi SOCK_STREAM khng th s dng cho multicasting. Khi mt socket ca loi SOCK_DGRAM c to, mt ng dng c th s dng hm setsockopt() iu khin cc c multicast km theo vi socket.Hm setsockopt() chp nhn vi c IPPROTO_IP: IP_ADD_MEMBERSHIP: tham gia nhm multicast IP_DROP_MEMBERSHIP: ri khi nhm multicast

Manlivo at3akma http://blog.sucmanhtinhoc.com

14

Tm hiu v lp trnh socket trong java IP_MULTICAST_IF: thit lp interface qua cc multicast datagram cn c chuyn i. IP_MULTICAST_TTL: thip lp Time To Live (TTL) trong IP header cho cc multicast datagram chun b chuyn i. IP_MULTICAST_LOOP:

8. File data transfer-send_file() v accept_and_recv() OS/400 cung cp hm send_file() v accept_and_recv() APIs cho vic truyn file nhanh hn v d dng hn qua kt ni sockets. Hai APIs c bit hu dng cho cc ng dng lin quan n file l cc HTTP server. Hm send_file() cho php gi trc tip file d liu t mt file h thng qua kt ni socket vi gi API Hm accept_and_recv() l mt s kt hp ca 3 hm accept(), gtsockname() v recv(). 9. Out-of-band data D liu Out-of-band (OOB) l dng d liu ngi dng c bit ch cho kt ni nh hng (stream) socket. Lung d liu ni chung nhn trong 1 th t ging nhau khi c gi. D liu OOB nhn c c lp so vi v tr trong stream (c lp so vi th t khi n c gi). iu c th bi v d liu , khi n c gi t chng trnh A ti chng trnh B, chng trnh B phi bo khi n ti ni. D liu OOB ch h tr cho AF_INET (SOCK_STREAM) v AF_INET6 (SOCK_STREAM) D liu OOB c gi bi c MSG_OOB trn hm send(), sendto() v sendmsg() S truyn d liu OOB cng ging nh vic truyn d liu thng thng. N gi sau khi bt k d liu c truyn vo b nh m. 10. I/O multiplexingselect() Bi v ng b vo ra cung cp a cch hiu qu s dng ti a ti nguyn ng dng ca bn., chng ta s dng ng b vo ra APIs hn l hm select() API. Tuy nhin, cc ng dng thit k cho php hm select() c s dng. Ging nh ng b vo ra I/O, select() to 1 im thng thng i trong nhiu hon cnh mi thi im ging nhau. Tuy nhiu hm select() cho php ng dng: Nhn thy nu d liu c th c Nhn thy nu d liu c th vit Nhn thy nu 1 trng hp ngoi l hin din

Manlivo at3akma http://blog.sucmanhtinhoc.com

15

Tm hiu v lp trnh socket trong java Hm select() cn cho php ng dng nu n mun i cho d liu sn sng (c hiu lc). ng dng c th cho php bao lu i 11. Socket network funtions Hm socket network cho php chng trnh ng dng thu c cc thng tin t host, protocol, service, v network file. Cc thng tin c th c truy xut bi tn v a ch hoc bi s truy cp tun t ca file. Cc hm network l bt buc khi ci t truyn thng gia chng trnh chy trn mng. V thc hin: Sp t cc host name ti cc a ch trn mng. Sp t cc network name ti s mng (network number) Sp t tn cc giao thc vi s hiu giao thc Sp t tn dch v vi s hiu cng. nh dng th t byte ca a ch mng internet nh dng a ch internet vi cc k hiu thp phn c du chm phn cch

III, Lp trnh socket trong Java 1. Cc th vin cho vic lp trnh Java.io Java.net

Trong java.net c cc lp thc hin cho vic lp trnh: ContentHandler DatagramPacket DatagramSocket InetAddress MulticastSocket ServerSocket Socket SocketImpl URL URLConnection URLEncoder URLStreamHandler 2. Exception trong java BindException ConnectException MalformedURLException NoRouteToHostException

Manlivo at3akma http://blog.sucmanhtinhoc.com

16

Tm hiu v lp trnh socket trong java ProtocolException SocketException UnknownHostException UnknownServiceException 3. Truyn tin vi giao thc TCP

Manlivo at3akma http://blog.sucmanhtinhoc.com

17

Tm hiu v lp trnh socket trong java Server To i tng SocketServer lng nghe kt ni t my khch gi ti cng (vd: 8080) Code: ServerSocket ss = new ServerSocket(8080); System.out.println ("Server is listenning..."); Ch i kt ni t Client: Code: Socket s = ss.accept(); System.out.print("Server has connected!\n"); //Nhn, gi v hin th thng tin PrintWriter out = new PrintWriter(s.getOutputStream(), true); BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream())); while(true) { // Receive System.out.print("Client: "); if(dataOut.equals("quit")) break; while (!in.ready()) {} System.out.println(in.readLine()); // Read one line and output it // Send System.out.print("Server: " ); BufferedReader kbin = new BufferedReader(new InputStreamReader(System.in)); dataOut = kbin.readLine(); out.println(dataOut); } ng kt ni v bt cc li.: Code: // Close out.close(); in.close(); s.close(); ss.close(); // Bt li: try {} catch(Exception e) { System.out.print("Error\n"); }

Client: Da vo IP ca my server v Port thc hin vic kt ni Code:

Manlivo at3akma http://blog.sucmanhtinhoc.com

18

Tm hiu v lp trnh socket trong java

Socket s = new Socket("192.168.1.100", 8080); Code th hin a ch IP ca my ch l 192.168.1.100 v kt ni thng qua cng 8080 //Nhn, gi v hin th thng tin PrintWriter out = new PrintWriter(s.getOutputStream(), true); BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream())); while(true) { // Send System.out.print("Client: " ); BufferedReader kbin = new BufferedReader(new InputStreamReader(System.in)); dataOut = kbin.readLine(); out.println(dataOut); // Receive System.out.print("Server: "); if(dataOut.equals("quit")) break; while (!in.ready()) {} System.out.println(in.readLine()); // Read one line and output it System.out.print("\n"); } ng v bt cc li: // Close out.close(); in.close(); s.close(); //Bt li try {} catch(Exception e) { System.out.print("Error!\n"); } 4. Truyn tin vi giao thc UDP

Manlivo at3akma http://blog.sucmanhtinhoc.com

19

Tm hiu v lp trnh socket trong java

UDPsocket Server: Khi to i tng Datagram Socket Code: int port = 8080; socket = new DatagramSocket(port); To buffer cho dng d liu nhp vo: Code byte[] buffer = new byte[1024]; To kiu i tng dng Datagram Socket cho dng d liu nhp vo: Code DatagramPacket packet = new DatagramPacket(buffer, buffer.length ); Ch d liu t Client:

Manlivo at3akma http://blog.sucmanhtinhoc.com

20

Tm hiu v lp trnh socket trong java Code: socket.receive(packet); Nhn d liu t Client: Code: InetAddress client = packet.getAddress(); int client_port = packet.getPort(); String s=new String(packet.getData(),0,packet.getLength()); Hin th vo Frame ca server: Code: TextArea ta1=new TextArea(); .. ta1.append(" Received "+s+" from "+client); Thc hin ng kt ni: Code: socket.close(); ta1.append("\nServer is stopped\n"); Client: To i tng kiu socket: Code: DatagramSocket clientSocket = new DatagramSocket(); To buffer cho d liu xut: Code: byte[] sendData = new byte[1024]; To gi d liu xut: Code DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 8080); Gi d liu: Code: clientSocket.send(sendPacket); Tng t vi nhn d liu truyn v: Code: byte[] receiveData = new byte[1024]; DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); clientSocket.receive(receivePacket); ng kt ni: Code: clientSocket.close(); 5. Kt lun V thi gian c hn nn ti liu ny cha c thc hin y mong cc c gi b qua.

Manlivo at3akma http://blog.sucmanhtinhoc.com

21

You might also like