version 1.1.1.1, 1999/10/08 02:12:05 |
version 1.4, 2001/08/12 03:13:36 |
|
|
|
/* $OpenXM: OpenXM/src/kan96xx/plugin/sm1Socket.c,v 1.3 2001/05/04 01:06:30 takayama Exp $ */ |
/* msg0s.c */ |
/* msg0s.c */ |
#include <stdio.h> |
#include <stdio.h> |
#include <sys/types.h> |
#include <sys/types.h> |
Line 57 struct object KsocketOpen(struct object obj) { |
|
Line 58 struct object KsocketOpen(struct object obj) { |
|
me.sin_family = AF_INET; |
me.sin_family = AF_INET; |
me.sin_port = htons(portNumber); |
me.sin_port = htons(portNumber); |
bcopy(myhost->h_addr, |
bcopy(myhost->h_addr, |
&me.sin_addr,myhost->h_length); |
&me.sin_addr,myhost->h_length); |
|
|
if ((s_waiting = socket(AF_INET,SOCK_STREAM,0)) < 0) { |
if ((s_waiting = socket(AF_INET,SOCK_STREAM,0)) < 0) { |
errorMsg1s("Socket allocation is failed."); |
errorMsg1s("Socket allocation is failed."); |
Line 81 struct object KsocketOpen(struct object obj) { |
|
Line 82 struct object KsocketOpen(struct object obj) { |
|
errorMsg1s("Listen failed"); |
errorMsg1s("Listen failed"); |
} |
} |
/* |
/* |
fprintf(stderr,"Done the initialization. port =%d\n",ntohs(me.sin_port)); |
fprintf(stderr,"Done the initialization. port =%d\n",ntohs(me.sin_port)); |
fprintf(stderr,"socket id = %d",accept(s_waiting,NULL,NULL)); */ |
fprintf(stderr,"socket id = %d",accept(s_waiting,NULL,NULL)); */ |
rob = newObjectArray(2); |
rob = newObjectArray(2); |
putoa(rob,0,KpoInteger(s_waiting)); |
putoa(rob,0,KpoInteger(s_waiting)); |
putoa(rob,1,KpoInteger(ntohs(me.sin_port))); |
putoa(rob,1,KpoInteger(ntohs(me.sin_port))); |
Line 131 struct object KsocketConnect(struct object obj) { |
|
Line 132 struct object KsocketConnect(struct object obj) { |
|
server.sin_family = AF_INET; |
server.sin_family = AF_INET; |
server.sin_port = htons(portNumber); |
server.sin_port = htons(portNumber); |
bcopy(servhost->h_addr, |
bcopy(servhost->h_addr, |
(char *)&server.sin_addr,servhost->h_length); |
(char *)&server.sin_addr,servhost->h_length); |
|
|
if ((socketid = socket(AF_INET,SOCK_STREAM,0)) <0) { |
if ((socketid = socket(AF_INET,SOCK_STREAM,0)) <0) { |
errorMsg1s("socket allocation is failed.\n"); |
errorMsg1s("socket allocation is failed.\n"); |
Line 236 struct object KsocketSelect(struct object obj) { |
|
Line 237 struct object KsocketSelect(struct object obj) { |
|
} |
} |
|
|
struct object KsocketSelectMulti(struct object obj) |
struct object KsocketSelectMulti(struct object obj) |
/* [ [integer socketid1, integer socketid2, ...] |
/* [ [integer socketid1, integer socketid2, ...] |
optional integer timeout default 0] |
optional integer timeout default 0] |
[ result1, result2, ....] |
[ result1, result2, ....] |
*/ |
*/ |
Line 349 struct object KsocketRead(struct object obj) { |
|
Line 350 struct object KsocketRead(struct object obj) { |
|
if (datasize - totalsize > 0) { |
if (datasize - totalsize > 0) { |
n = read(socketid,data+totalsize,datasize-totalsize); |
n = read(socketid,data+totalsize,datasize-totalsize); |
if (n < 0) { |
if (n < 0) { |
errorMsg1s("Read error."); |
errorMsg1s("Read error."); |
} |
} |
if (n < datasize-totalsize) { |
if (n < datasize-totalsize) { |
totalsize += n; |
totalsize += n; |
break; |
break; |
}else{ totalsize += n; } |
}else{ totalsize += n; } |
if (totalsize == 0) { |
if (totalsize == 0) { |
errorMsg1s("Select returns 1, but there is no data to read."); |
errorMsg1s("Select returns 1, but there is no data to read."); |
} |
} |
}else { /* increase the datasize */ |
}else { /* increase the datasize */ |
tmp = (char *)GC_malloc(sizeof(char)*2*datasize); |
tmp = (char *)GC_malloc(sizeof(char)*2*datasize); |
Line 475 struct object KsocketWriteByte(struct object obj) { |
|
Line 476 struct object KsocketWriteByte(struct object obj) { |
|
struct object ob1; |
struct object ob1; |
struct object ob2; |
struct object ob2; |
int socketid; |
int socketid; |
int r; |
int r,i,n,kk,r0; |
char data[2]; |
#define DATA_SIZE 1024 |
|
char data[DATA_SIZE]; |
if (obj.tag != Sarray) { |
if (obj.tag != Sarray) { |
errorMsg1s("KsocketWriteByte([integer socketid, int])"); |
errorMsg1s("KsocketWriteByte([integer socketid, int | array of int])"); |
} |
} |
if (getoaSize(obj) < 2) { |
if (getoaSize(obj) < 2) { |
errorMsg1s("KsocketWriteByte([integer socketid, int])"); |
errorMsg1s("KsocketWriteByte([integer socketid, int | array of int])"); |
} |
} |
ob1 = getoa(obj,0); |
ob1 = getoa(obj,0); |
ob2 = getoa(obj,1); |
ob2 = getoa(obj,1); |
if (ob1.tag != Sinteger) { |
if (ob1.tag != Sinteger) { |
errorMsg1s("KsocketWriteByte([integer socketid, int]) : the first argument must be an integer."); |
errorMsg1s("KsocketWriteByte([integer socketid, int | array of int]) : the first argument must be an integer."); |
} |
} |
socketid = KopInteger(ob1); |
socketid = KopInteger(ob1); |
if (ob2.tag != Sinteger) { |
if (ob2.tag != Sinteger && ob2.tag != Sarray) { |
errorMsg1s("KsocketWriteByte([integer socketid, int]) : the second argument must be a string."); |
errorMsg1s("KsocketWriteByte([integer socketid, int | array of int]) : the second argument must be an integer or an array of integers."); |
} |
} |
data[0] = KopInteger(ob2); |
if (ob2.tag == Sinteger) { |
r = write(socketid,data, 1); |
data[0] = KopInteger(ob2); |
|
r = write(socketid,data, 1); |
|
}else{ |
|
n = getoaSize(ob2); kk = 0; r = 0; |
|
for (i=0; i<n; i++) { |
|
if (getoa(ob2,i).tag != Sinteger) |
|
errorMsg1s("KsocketWriteByte([integer socketid, int | array of int]) : elements of the second argument must be integers."); |
|
data[kk] = KopInteger(getoa(ob2,i)); |
|
kk++; |
|
if (kk >= DATA_SIZE) { |
|
r0 = write(socketid,data,kk); |
|
if (r0 != kk) { |
|
fprintf(stderr,"Warning: Could not write to the socket.\n"); |
|
return(KpoInteger(r+r0)); |
|
} |
|
r += r0; |
|
kk = 0; |
|
} |
|
} |
|
if (kk > 0) { |
|
r0 = write(socketid,data,kk); |
|
if (r0 != kk) { |
|
fprintf(stderr,"Warning: Could not write to the socket.\n"); |
|
return(KpoInteger(r+r0)); |
|
} |
|
r += r0; |
|
} |
|
} |
return(KpoInteger(r)); |
return(KpoInteger(r)); |
} |
} |
|
|