Tampilkan postingan dengan label Programming. Tampilkan semua postingan
Tampilkan postingan dengan label Programming. Tampilkan semua postingan

Selasa, 24 Maret 2009

Socket Programming in C-Client

Client socket program in C
Code:

/***************************************************************************

main.c - description
-------------------
begin : Wed Aug 4 13:45:50 WIT 2004
copyright : (C) |YEAR| by Ru2004awan
email : rgunawan@yahoo.com
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "unp.h"

main(int argc, char **argv)
{
char server_addr[30];
int commsock, md, mst, port;

if (argc <>
fprintf(stderr, "Usage: client_socket_standard \n");
return;
} else {
strcpy(server_addr, argv[1]);
sscanf(argv[2], "%d", &port);
//sscanf(argv[3], "%d", &md);
//sscanf(argv[4], "%d", &mst);
init_sock(server_addr, port, &commsock);
send_to_socket(commsock);
//nb_send(commsock, md, mst);
close(commsock);
}
}

init_sock(char *server_addr, int port, int *commsock)
{
struct sockaddr_in servaddr;
int x;

/********************* Initialisasi client socket *****************/

*commsock = socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(port);
inet_aton(server_addr, &servaddr.sin_addr);

x = connect(*commsock, (SA *) &servaddr, sizeof(servaddr));
fprintf(stderr, "server connected commsock = %d\n", *commsock);

/******************************************************************/
}

send_to_socket(int commsock)
{
unsigned char tmp[1000], buf[1000], repbuf[1000];
int nread, nwrite, l;

while (1) {
fprintf(stderr, "Start of loop\n ");
fgets(buf, 200, stdin);
l = strlen(buf);
nwrite = write(commsock, buf, l);
fprintf(stderr, "write %d bytes to server\n", nwrite);

nread = read(commsock, repbuf, 1000);
repbuf[nread] = 0;
if (!nread) {
fprintf(stderr, "server connection close\n");
break;
} else
fprintf(stderr, "server_reply : %s\n", repbuf);
}
}


Header File unp.h



#ifndef __unp_h
#define __unp_h


#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

// #ifdef HAVE_SYS_SELECT_H
#include
// #endif

// #ifdef HAVE_POLL_H
#include
// #endif

// #ifdef HAVE_STRING
#include
// #endif
// #ifdef HAVE_SYS_IOCTL_H
#include
// #endif
// #ifdef HAVE_SYS_FILIO_H
// #include
// #endif
// #ifdef HAVE_SYS_SOCKIO_H
// #include

// #ifdef HAVE_PTHREAD_H
#include
// #endif

#ifdef __osf__
#undef recv
#undef send
#define recv(a,b,c,d) recvfrom(a,b,c,d,0,0)
#define send(a,b,c,d) sendto(a,b,c,d,0,0)
#endif

#ifndef INADDR_NONE
#define INADDR_NONE 0xffffffff
#endif

#ifndef SHUT_RD
#define SHUT_RD 0
#define SHUT_WR 1
#define SHUT_RDWR 2
#endif

#ifndef INET_ADDRSTRLEN
#define INET_ADDRSTRLEN 16
#endif

#ifndef INET6_ADDRSTRLEN
#define INET6_ADDRSTRLEN 46
#endif

#ifndef HAVE_BZERO
#define bzero(ptr,n) memset(ptr,0,n)
#endif

#ifndef HAVE_GETHOSTBYNAME2
#define gethostbyname2(host,family) gethostbyname((host))
#endif

// struct in_pktinfo {
// struct in_addr ipi_addr;
// int ipi_ifindex;
// };

#ifndef CMSG_LEN
#define CMSG_LEN(size) (sizeof(struct cmsghdr) + (size))
#endif
#ifndef CMSG_SPACE
#define CMSG_SPACE(size) (sizeof(struct cmsgdhr) + (size))
#endif

#ifndef SUN_LEN
#define SUN_LEN(su) \
(sizeof(*(su)) - sizeof((su)->sun_path) + strlen((su)->sun_path))
#endif

#ifndef AF_LOCAL
#define AF_LOCAL AF_UNIX
#endif
#ifndef PF_LOCAL
#define PF_LOCAL PF_UNIX
#endif

#ifndef INFTIM
#define INFTIM (-1)
// #ifdef HAVE_POLL_H
// ##define INFTIM_UNPH
// #endif
#endif

#define LISTENQ 1024
#define MAXLINE 4096
#define MAXSOCKADDR 128
#define BUFFSIZE 8192

#define SERV_PORT 9877
#define SERV_PORT_STR *9877*
#define UNIXSTR_PATH */tmp/unix.str*
#define UNIXDG_PATH */tmp/unix.dg*

#define SA struct sockaddr
#define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
#define DIR_MODE (FILE_MODE | S_IXUSR | S_IXGRP | S_IXOTH)

typedef void Sigfunc (int);

#define min(a,b) ((a) < (b) ? (a) : (b))
#define max(a,b) ((a) > (b) ? (a) : (b))

// #ifndef HAVE_ADDRINFO_STRUCT

// #ifndef HAVE_TIMESPEC_STRUCT
//struct timespec {
//time_t tv_sec;
//long tv_nsec;
//};

#endif

Jumat, 20 Maret 2009

Mengatur Spasi Baris Printing di VB6

Ringkasan:
Object Printer tidak secara khusus mempunyai seting untuk mengatur spasi baris, tetapi kita bisa mengatur spasi baris dengan menggunakan properti CurrentY dari object Printer.


Informasi Lebih Lanjut
Posisi print dari suatu halaman ditentukan oleh dua buah properti dari object Printer, CurrentX dan CurrentY, yang akan mengatur koordinat X dan Y . Setiap kali anda memanggil perintah Printer.Print, posisi CurrentY selalu bertambah untuk persiapan pencetakan baris selanjutnya. Jumlah penambahan ditentukan oleh jenis font dan driver dari printer. Jadi untuk memanipulasi spasi baris kita bisa gunakan secara langsung Printer.CurrentY.

Secara default, properti ini menggunakan twips dan besarnya 1440 twips tiap inchi logic. Jadi untuk enam baris per inchi kita membutuhkan 240 twips, dan untuk delapan baris per inchi kita menggunakan 180 twips. Kita bisa set Printer.ScaleMode untuk menggunakan predefine skala yang berbeda atau set properti ScaleHeight dan ScaleWidth untuk membuat skala sendiri. Ini juga bisa membantu untuk menggunakan properti TextHeight untuk menetukan tinggi minimum dari text, dan TextWidth untuk menentukan lebar dari string. Silahkan dilihat kembali Online Help dan lihat bagian REFERENCES di bawah untuk informasi lebih lanjut menggunakan properti-properti ini.
Code program di bawah ini menunjukan efek dari perubahan properti antar baris CurrentY. Sebagai catatan dengan cara ini kita bisa membuat pencetakan yang tumpang tindih tanpa error.

Contoh Langkah-Demi-Langkah
1. Start a new Standard EXE project. Form1 is created by default.
2. Add the following code to the code window of Form1:

Option Explicit

Private Sub Form_Click()
Dim Lines As Integer, Y As Long, OutStr As String

With Printer
.Print "Font is " & .Font.Name & " " & .Font.Size & " pt"

.CurrentY = .CurrentY + 500 ' Skip some space
.Print "First, print normally ..."
.Print "Line 1 " & ".CurrentY = " & .CurrentY
.Print "Line 2 " & ".CurrentY = " & .CurrentY
.Print "Line 3 " & ".CurrentY = " & .CurrentY

.CurrentY = .CurrentY + 500 ' Skip some space
.Print "Now shorten the lines for less line spacing ..."
.Print "Line 1 " & ".CurrentY = " & .CurrentY
.CurrentY = .CurrentY - 50
.Print "Line 2 " & ".CurrentY = " & .CurrentY
.CurrentY = .CurrentY - 50
.Print "Line 3 " & ".CurrentY = " & .CurrentY

.CurrentY = .CurrentY + 500 ' Skip some space
.Print "Now increase line spacing ..."
.CurrentY = .CurrentY + 50
.Print "Line 1 " & ".CurrentY = " & .CurrentY
.CurrentY = .CurrentY + 50
.Print "Line 2 " & ".CurrentY = " & .CurrentY
.CurrentY = .CurrentY + 50
.Print "Line 3 " & ".CurrentY = " & .CurrentY

.CurrentY = .CurrentY + 500 ' Skip some space
.Print "Now specify 6 lines per inch ..."
' Start at an even position to better see increments.
.CurrentY = .CurrentY + (1000 - .CurrentY Mod 1000)
For Lines = 1 To 6 ' These 6 lines take 1 inch on the page.
Y = .CurrentY ' Remember current Y position.
.Print "Line " & Lines & ": " & "6 Lines per inch - " & _
".CurrentY = " & .CurrentY
' Increment the Y position by 1/6th of an inch (240 twips).
.CurrentY = Y + 240 ' Reset to previous position + 240.
Next Lines
.EndDoc ' End the print job.
End With
End Sub

Private Sub Form_Load()
' Select a standard Font and size (Optional)
Printer.Font.Name = "Arial"
Printer.Font.Size = 8
End Sub



3. Jalankan program di atas akan menunjukan variasi dari spasi baris yang berbeda

Cara Printing Text ke Printer di VB6

Printing di Visual basic 6 menggunakan object Printer.print, setiap text/string yang akan di print langsung dengan perintah Printer.print "Text ke Pinter" , setelah selesai ngeprint printer akan langsung ganti baris. Printer akan memulai mencetak setelah kita berikan perintah Printer.Endoc.
Pengaturan font bisa dilakukan dengan perintah Printer.font.name = "Courier new" dan ukuran huruf Printer.Font.Size = 10
Courier New adalah jenis huruf yang ukuran karakternya sama termasuk spasi, jadi kalau ingin membuat cetakan rapih seperti tabel gunakan jenis huruf ini, yang lain tidak(tahu).

Contoh:
Private Sub cmdPrint_Click()
Printer.FontSize = 9
Printer.Font.Name = "Courier new"
Printer.Print "Baris pertama nomor 1"
Printer.Print "Baris Kedua nomor 2"
Printer.Print "Baris Ketiga nomor 3"
Printer.EndDoc
End Sub

PHP File Upload

________________________________

Dengan PHP, memungkinkan upload file ke server .
________________________________

Buat Form Upload-File
Membuat pengguna untuk dapat meyimpan file di server lewat formulir akan sangat menolong.
Perhatikan bentuk HTML untuk uploading files:

<html>
<body>
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" >
<br >
<input type="submit" name="submit" value="Submit" >
</form>
</body>
</html>

Catatan mengenai form HTML di atas :
* Atribut enctype attribute dari tag <form> menentukan content-type yang mana yg digunakan untuk mengirim bentuk data . "multipart/form-data" digunakan ketika binary data diperlukan, seperti isi dari file yg ingi di-upload.
* Atribut type="file" dari <input> menunjukan bahwa input harus diproses sebagai file. Sebagai contoh ketika dilihat oleh browser akan ada tombol browse disebelah input field.
Note: Mengijinkan pengguna untuk upload file adalah masalah kemanan yang cukup beresiko. Hanya pengguna yang terpercaya yang diijinkan melakukan upload file.

________________________________

Buat Script untuk menangkap perintah Upload
File "upload_file.php" berisi code untuk uploading a file:
<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
}
?>
Dengan menggunakan global array PHP $_FILES kita dapat meng- upload file dari komputer client ke remote server.
Parameter pertama dari nama form input dan yang indeks kedua bisa "name", "type", "size", "tmp_name" atau "error". Seperti ini:
* $_FILES["file"]["name"] - the name of the uploaded file
* $_FILES["file"]["type"] - the type of the uploaded file
* $_FILES["file"]["size"] - the size in bytes of the uploaded file
* $_FILES["file"]["tmp_name"] - the name of the temporary copy of the file stored on the server
* $_FILES["file"]["error"] - the error code resulting from the file upload
Ini merupakan cara yang paling sederhana untuk meng-upload file. Untuk keperluan keamanan, kita seharusnya membatasi pengguna file jenis apa saja yang diijinkan untuk di upload.
________________________________

Pembatasan Ukuran dalam Upload
Pada script ini kita menambahkan pembatasan file yang akan di upload. Pengguna bisa mengupload file .gif or .jpeg dan ukuran file harus dibawah 20 kb:
<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
}
}
else
{
echo "Invalid file";
}
?>
Note: Pada IE file jpg dikenali sebagai pjpeg, untuk FireFox harus jpeg.
________________________________

Menyimpan File yang terUpload
Contoh di atas membuat copy sementara dari file yang ter-upload di PHP folder temp di server.
File copy di folder sementara akan hilang setelah script selesai. Untuk menyimpannya kita harus meng-copy-nya ke lokasi berbeda.
<?php
if (($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
&& ($_FILES["file"]["size"] < 20000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
Script di atas akan mencek apakah file sudah ada, jika tidak akan meng-copynya.

Cara Printing Text ke Printer di Delphi

Cara sederhana untuk mencetak text ke printer sbb:

Delphi Print

1. // send the lines of a multiline editbox (memo) to the printer
2. // the form contains at least three components:
3. // a Button, a MemoBox named TotalCost and a PrintDialog
4. // note that BLANK is just a space character
5. // Delphi 3.0
6.
7. procedure TForm1.PrintTotalButtonClick(Sender: TObject);
8. var
9. POutput: TextFile;
10. k: Integer;
11. begin
12. if PrintDialog1.Execute then
13. begin
14. AssignPrn(POutput);
15. Rewrite(POutput);
16. Printer.Canvas.Font.Name := 'Courier New';
17. Printer.Canvas.Font.Size := 8;
18. Writeln(POutput,BLANK);
19. Writeln(POutput,BLANK);
20. Writeln(POutput,BLANK);
21. for k := 0 to TotalCost.Lines.Count - 1 do
22. Writeln(POutput, ' ' + TotalCost.Lines[k]);
23. CloseFile(POutput);
24. end;
25. end;

Kamis, 19 Maret 2009

Menggunakan Form HTML dengan PHP

Ketka sebuah form di submit ke PHP script, informasi foram akan secara otomatis tersedia untuk script. Ada beberapa cara untuk mengakses informasi contoh:

Contoh#1 Sebuah form HTML sederhana
< action="foo.php" method="post">
Name: < name="username">
Email: < name="email">
< type="submit" value="Submit me!" name="submit">

Tergantung dari setup dan preferenc personal, ada banyak cara untuk mengkases data dari form HTML anda salah satunya adalah sebagai berikut :

Contoh#2 Accessing data from a simple POST HTML form

Menggunakan form GET serupa.