146 lines
2.9 KiB
C
146 lines
2.9 KiB
C
#include <stdio.h>
|
|
#include <strings.h>
|
|
|
|
#include <proto/exec.h>
|
|
#include <proto/dos.h>
|
|
|
|
#include <exec/memory.h>
|
|
|
|
#include <proto/socket.h>
|
|
#include <arpa/inet.h>
|
|
#include <netinet/in.h>
|
|
|
|
struct Library *SocketBase;
|
|
char *ReadBuffer;
|
|
int SocketID = -1;
|
|
|
|
#define READ_BUFFER_SIZE (1024)
|
|
|
|
void teardown() {
|
|
if (SocketBase && SocketID != -1) {
|
|
CloseSocket(SocketID);
|
|
}
|
|
|
|
if (ReadBuffer) {
|
|
FreeMem(ReadBuffer, READ_BUFFER_SIZE);
|
|
}
|
|
|
|
if (SocketBase) {
|
|
CloseLibrary(SocketBase);
|
|
}
|
|
}
|
|
|
|
int main(void) {
|
|
int ReadItemResult;
|
|
ULONG IPAddress;
|
|
long Port;
|
|
char CurrentChar;
|
|
int ReadIndex;
|
|
BPTR Stdin;
|
|
|
|
struct sockaddr_in ConnectSockaddrIn;
|
|
|
|
if (!(SocketBase = OpenLibrary("bsdsocket.library", 4))) {
|
|
printf("Unable to open bsdsocket.library. Have you started a TCP stack?\n");
|
|
return 1;
|
|
}
|
|
|
|
if (!(ReadBuffer = AllocMem(READ_BUFFER_SIZE, MEMF_CLEAR))) {
|
|
printf("Unable to allocate buffer\n");
|
|
teardown();
|
|
return 1;
|
|
}
|
|
|
|
// yes we could use argc/argv but we're mimicking what's in the
|
|
// original assembler code
|
|
|
|
ReadItemResult = ReadItem(ReadBuffer, READ_BUFFER_SIZE, NULL);
|
|
if (
|
|
ReadItemResult == ITEM_ERROR ||
|
|
ReadItemResult == ITEM_NOTHING
|
|
) {
|
|
printf("client <ip address> <port> <message...>\n");
|
|
teardown();
|
|
return 1;
|
|
}
|
|
|
|
if (!(IPAddress = inet_addr(ReadBuffer))) {
|
|
printf("Unable to parse IP Address\n");
|
|
teardown();
|
|
return 1;
|
|
}
|
|
|
|
ReadItemResult = ReadItem(ReadBuffer, READ_BUFFER_SIZE, NULL);
|
|
if (
|
|
ReadItemResult == ITEM_ERROR ||
|
|
ReadItemResult == ITEM_NOTHING
|
|
) {
|
|
printf("client <ip address> <port> <message...>\n");
|
|
teardown();
|
|
return 1;
|
|
}
|
|
|
|
if (-1 == (StrToLong(ReadBuffer, &Port))) {
|
|
printf("client <ip address> <port> <message...>\n");
|
|
teardown();
|
|
return 1;
|
|
}
|
|
|
|
if (Port > 65535) {
|
|
printf("Port out of range (0-65535)\n");
|
|
teardown();
|
|
return 1;
|
|
}
|
|
|
|
Stdin = Input();
|
|
|
|
Port = (short)Port;
|
|
CurrentChar = FGetC(Stdin);
|
|
ReadIndex = 0;
|
|
|
|
while (CurrentChar != -1 && CurrentChar != 10) {
|
|
ReadBuffer[ReadIndex++] = CurrentChar;
|
|
printf("%d\n",CurrentChar);
|
|
CurrentChar = FGetC(Stdin);
|
|
}
|
|
|
|
ReadBuffer[ReadIndex++] = 10;
|
|
ReadBuffer[ReadIndex++] = 0;
|
|
|
|
SocketID = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
|
|
|
|
// we'll skip on using RawDoFmt here
|
|
printf(
|
|
"IP address is %8x, port is %d, socket is %d\n",
|
|
IPAddress,
|
|
Port,
|
|
SocketID
|
|
);
|
|
|
|
if (SocketID == -1) {
|
|
printf("Unable to open socket\n");
|
|
teardown();
|
|
return 1;
|
|
}
|
|
|
|
ConnectSockaddrIn.sin_family = AF_INET;
|
|
ConnectSockaddrIn.sin_port = Port;
|
|
ConnectSockaddrIn.sin_addr.s_addr = IPAddress;
|
|
|
|
if (connect(SocketID, (struct sockaddr *)&ConnectSockaddrIn, sizeof(struct sockaddr_in)) == -1) {
|
|
printf("Unable to connect socket\n");
|
|
teardown();
|
|
return 1;
|
|
}
|
|
|
|
if (send(SocketID, ReadBuffer, ReadIndex, 0) == -1) {
|
|
printf("Unable to send data\n");
|
|
teardown();
|
|
return 1;
|
|
}
|
|
|
|
teardown();
|
|
return 0;
|
|
|
|
}
|